The standard music app can be controlled by MPMusicPlayerController.iPodMusicPlayer
.
var player=MediaPlayer.MPMusicPlayerController.iPodMusicPlayer;
if(player.PlaybackState==MediaPlayer.MPMusicPlaybackState.Playing)
{
player.Pause();
}
If it is not limited to music apps, you can do it with AVFoundation.AVAudioSession
.
var session=AVFoundation.AVAudioSession.SharedInstance();
NSError error;
session.SetActive(true, outerr);
AVAudioSession
gives you more control, such as "Turn down the volume of other apps and play your app's voice."
For Android, AudioManager.RequestAudioFocus
can stop playing other apps by taking away audio focus exclusively.
The following example takes the focus away when the app screen appears, stops playing other apps, and gives up when the app is in the background.
public class MainActivity: Activity, Android.Media.AudioManager.IonAudioFocusChangeListener
{
Android.Media.AudioManager_audioManager;
protected override void OnResume()
{
base.OnResume();
_audioManager= (Android.Media.AudioManager) GetSystemService (Context.AudioService);
// exclusively deprive someone of focus
_audioManager.RequestAudioFocus(this, Android.Media.Stream.Music, Android.Media.AudioFocus.GainTransientExclusive);
}
public void OnAudioFocusChange([GeneratedEnum] Android.Media.AudioFocus FocusChange)
{
// be called when one gets focus
}
protected override void OnPause()
{
// relinquish focus
_audioManager.AbandonAudioFocus(this);
base.OnPause();
}
}
Audio-related controls are complicated and difficult for each platform, so you should first learn about each Android/iOS API.
© 2025 OneMinuteCode. All rights reserved.