iOS enable to play audio after return from other music app

时间:2017-08-04 12:27:59

标签: ios swift uiviewcontroller avaudioplayer audio-player

I am working on music app.

Here is the case which I want to fix: I open AppleMusic app > play something > open my app > click play > music does not play. Why and how to fix it?

I use that but still does not work

func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        try AVAudioSession.sharedInstance().setActive(true)
    } catch let error as NSError {
        print(error.localizedDescription)
    }
    UIApplication.shared.beginReceivingRemoteControlEvents()
}

1 个答案:

答案 0 :(得分:0)

This code shows you if another app is playing sound in the backgroud

var otherAudioPlaying = AVAudioSession.sharedInstance().isOtherAudioPlaying

If so, try this:

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, error: nil)