我正在开发音乐应用程序。
这是我想要修复的情况: 我打开 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)
关于iOS 允许从其他音乐应用程序返回后播放音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45506869/