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
Play a Video from Youtube in a Avplayerviewcontroller in Swift
Avassetwriter Avvideoexpectedsourceframeratekey (Frame Rate) Ignored
Uipickerview: Nsattributedstring Not Available in iOS 7
Using iOS 6 Theme for iOS 7 App
How to Add Text, Shape and Signature in Photo Markup with Pencil Kit
Calling Instance Method During Initialization in Swift
Xcode iOS App Development Code Signing
Why Do Some Views Appear as a Red No Entry Sign in Widgets
Google Maps Sdk Not Displaying Properly in Uiview
Uiscrollview with Dynamically Sized Content
Fontawesome Pro and Xamarin.iOS Only One Font Can Be Active
Firebase Queryorderedbychild() Method Not Giving Sorted Data
Using Private API to Read Wifi Rssi Value
Ibm Worklight 6.1 - Failed Re-Generating the iPhone Native Folder
Nsstrikethroughstyleattributename , How to Strike Out the String in iOS 10.3