简体   繁体   English

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

[英]iOS enable to play audio after return from other music app

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. 这是我要修复的情况:我打开AppleMusic应用>播放某些内容>打开我的应用>单击播放>音乐不播放。 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()
}

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM