Mute sound in Xamarin.iOS (iOS version >11)

2019-07-25 18:25发布

问题:

I am using XamarinMediaManager (built using MPMusicPlayerController) to Build an audio player for iOS, i am looking for a way to mute the sound on a button click, this works perfectly on iOS 9:

MPMusicPlayerController.ApplicationMusicPlayer.Volume = 0 

But it looks like it is deprecated on newer versions: https://developer.apple.com/documentation/mediaplayer/mpmusicplayercontroller/1624567-volume

How to achieve the same thing using another way? (i am not looking to build an entire volume manager, i just want to set the volume).

Any help would be appreciated.

回答1:

According to the documentation posted it's been deprecated for MPVolumeView, which is their new slider view for adjusting audio output. I know you're not looking to add the slider to your UI and want to do it programmatically, which based on this post you can still technically do it. Although, it does feel kind of hacky.

Here is the C# equivalent:

UISlider slider = null;
var volumeView = new MPVolumeView();
foreach (var view in volumeView.Subviews)
{
    if (view is UISlider)
    {
        slider = (UISlider)view;
        break;
    }
}

slider.Value = 0;

It's worth noting I'm unsure whether if this will actually effect how XamarinMediaManager is playing the audio.