2

I am working on a MediaPlayer using Xamarin.iOS, i can't find a way to mute the sound when a button is clicked, something like this in Android:

AudioManager amanager = (AudioManager)GetSystemService(Context.AudioService);
amanager.SetStreamVolume(Stream.Music, 0, VolumeNotificationFlags.RemoveSoundAndVibrate);

Any help would be appreciated.

1 Answers1

1

If the sound you're wanting to mute is your own sound/music that you have in your iOS app itself and playing you can use AVAudioPlayer like so:

AVAudioPlayer _player;

public void PlayMusic(string file)
{
    NSError error;
    _player = new AVAudioPlayer(new NSUrl(file + ".mp3"), "mp3", out error);
    if (error != null)
    {
        Debu.WriteLine("Error in PlayTheme, {0}: {1}", file, error.LocalizedDescription);
        return;
    }
    _player.Volume = 0.6f;
    _player.NumberOfLoops = -1;
    _player.Play();
}

public void StopMusic()
{
    if (_player != null)
    {
        _player.Stop();
        _player.Dispose();
        _player = null;
    }
}

Also, depending on how you are wanting to control your audio you could use the MPMusicPlayerController.ApplicationMusicPlayer instead. Here is the Apple doc about it.

However, if you are looking to mute iOS system audio itself it is technically possible by using the private api, but by doing so I don't think Apple would approve your app when you try to release it in the app store.

Nick Peppers
  • 3,161
  • 23
  • 27
  • Thank you for you answer, i am using XamarinMediaManager to play audio, where they didn't implement the mute functionality yet, do you think that something like `MPMusicPlayerController.ApplicationMusicPlayer.Volume = 0` could do the trick ? –  May 24 '18 at 13:52
  • From what I'm seeing XamarinMediaManager on the iOS side seems to be creating an `AVPlayer` to play it's audio so what you might be able to do is cast `var player = MediaManager.AudioPlayer as AVPlayer` and set the volume on it. Although, you could just stop or pause the music instead of muting it. – Nick Peppers May 24 '18 at 14:13
  • Thank you @Nick, `MPMusicPlayerController.ApplicationMusicPlayer.Volume = 0 ` did indeed do the trick :)) –  May 24 '18 at 14:46
  • 1
    Nice glad to have helped. I haven't used the XamarinMediaManager plugin before so I didn't know what platform specific implementation it used for audio, which it looks like the MediaManager on iOS is indeed using the built-in `MPMusicPlayerController` – Nick Peppers May 24 '18 at 15:01
  • Hi @Nick, it looks like this doesn't work on newer versions, any idea: https://stackoverflow.com/questions/50532271/mute-sound-in-xamarin-ios-ios-version-11 –  May 25 '18 at 15:13
  • From your link it's says to use MPVolumeView class to adjust media playback instead of MPMusicPlayerController I'd try that, but my guess is even though it's been deprecated it still will probably work on newer versions. – Nick Peppers May 25 '18 at 15:19
  • 1
    I'd try the MPVolumeView then https://developer.apple.com/documentation/mediaplayer/mpvolumeview?language=objc whether that will actually work with XamarinMediaManager is a different question. – Nick Peppers May 25 '18 at 15:43
  • I don't like to have a slider control to set the volume or any predefined view (i just want to mute the sound and unmute it), how can achieve that using the `MPVolumeView` ? –  May 25 '18 at 15:47