如果我使用以下选项将我的应用程序的音频会话模式设置为AVAudioSessionCategoryPlayback:
AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers |
AVAudioSessionCategoryOptionDuckOthers
一切都很好,我的应用程序播放其音频与苹果音乐在后台躲避 .
但是,如果在更改为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];
我做任何不同的事情,也不是每次都激活它 . 我能找到的唯一一个允许我的应用播放音频和播放其他内容的方法是不在我的应用中的任何一点设置环境会话,但我需要在应用中的某些点播放环境音频,所以此解决方案不是'可行的 . 请帮忙 :)
1 回答
(Swift 4回答)在iOS 11上测试 - 在设置环境会话时你应该使用.duckOthers或.mixWithOthers
SessionDidFinish完成后,您可以设置不同的选项 . 如果您正在使用TTS,您还需要确保在调用sessionDidFinish之前没有在最后一个话语上设置.postUtteranceDelay . 使用AVSpeechSynthesizerDelegate方法捕获话语何时真正完成 .