How to Stop and Resume Background Audio from iPhone App

How to stop and resume background audio from iPhone app?

You cannot do this for all applications. For applications that use the iPod player in their applications:

Assuming your application is in foreground. You can do this:

MPMusicPlayerController *mp = [MPMusicPlayerController iPodMusicPlayer];        
[mp stop]; // or [mp pause]

This needs the MediaPlayer.framework and also #import <MediaPlayer/MediaPlayer.h>.

Read more at MPMusicPlayerController Reference.


For doing it through Audio Player Sessions you can use the property kAudioSessionCategory_SoloAmbientSound. Reference here.

This is similar to AVAudioSessionCategorySoloAmbient defined in AVAudioSession class.

Quoting from the documentation:

When you use this category, audio from other apps is silenced.

AVAudioPlayer resume from background but pause for other apps

Adding UIApplication.shared.beginReceivingRemoteControlEvents() after setting the category fixed this issue but I'm not sure why.

Resume Background Audio After Playing Sound With AVAudioPlayer

You need to properly implement the AVAudioSession delegate methods. If you set the audio session to inactive, then the system will know you are done playing your sound and will resume any other audio sessions. Refer to the Audio Session Programming Guide, especially the setActive:error: method.

How to resume background audio in Swift 2 / AVPlayer?

Finally got it!

Solution: added the mixable option by changing the setCategory line to be:

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback,
withOptions: .mixWithOthers )


Related Topics



Leave a reply



Submit