iOS支持从其他音乐应用程序返回后播放音频

问题描述:

我正在制作音乐应用程序。iOS支持从其他音乐应用程序返回后播放音频

这里是我想修复的情况: 我打开AppleMusic应用程序>玩东西>打开我的应用程序>点击播放>音乐不播放。为什么以及如何解决它?

我用,但仍然无法正常工作

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() 
} 

此代码显示,如果另一个应用是在研究背景

var otherAudioPlaying = AVAudioSession.sharedInstance().isOtherAudioPlaying 

如果是播放声音,试试这个:

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