如果我使用以下选项将我的应用程序的音频会话模式设置为 AVAudioSessionCategoryPlayback:
AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers |
AVAudioSessionCategoryOptionDuckOthers
Run Code Online (Sandbox Code Playgroud)
一切正常,我的应用程序在后台播放 Apple 音乐时播放音频。
但是,如果我的应用程序的音频会话类别在更改为 AVAudioSessionCategoryPlayback 之前是 AVAudioSessionCategoryAmbient,Apple 音乐(和其他音乐应用程序)将停止播放。
这是我的代码(我检查错误并返回值,但为了清楚起见,我删除了代码段中的这些部分:
// If I comment out this call, my app will duck others...
// If it is commented in, rather than ducking, my app stops all other audio
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient
withOptions:0
error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
// Do other stuff in the app and at some point the following code gets called
theOptions = AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers |
AVAudioSessionCategoryOptionDuckOthers
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
withOptions:theOptions
error:nil];
Run Code Online (Sandbox Code Playgroud)
我试过在更改之间停用会话,但这并没有任何不同,也不会每次都激活它。我唯一能找到的允许我的应用程序播放音频并避开其他人的事情是不在我的应用程序中的任何点设置环境会话,但我需要在应用程序的某些点播放环境音频,因此此解决方案不是“可行。请帮忙 :)