我正在开发一个使用 AVSpeechSynthesizer 来指示方向的导航应用程序。如果我正在播放播客,我希望能够在说出指示时暂停音频,然后再继续播放。这是使用以下方法成功实现的:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers
error:nil];
但是,如果我使用上述代码播放音乐,则指令会与音乐混合在一起,而音乐的音量不会降低——因此很难听到指令。所以我尝试了:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionDuckOthers
error:nil];
现在背景音乐的音量降低了,这正是我想要的,但是当我播放语音音频(即播客)时,它不再暂停,而是与指令混合 - 尽管音量降低了。
我真正想要的是:AVAudioSessionCategoryOptionInterruptSpokenAudioAndDuckWithOthers。然而,那是不存在的。我知道这种组合是可能的,因为其他应用程序(如 Waze)有这种行为。
我知道您可以使用以下方法检测音频何时播放:
BOOL isOtherAudioPlaying = [[AVAudioSession sharedInstance] isOtherAudioPlaying];
但是,我找不到一种方法来确定该音频是否被朗读。如果可以的话,我可以根据任何给定时间正在播放的内容进行设置。任何人都可以帮助解决这个问题,最好是在 Objective C 中吗?
最佳答案
是这样的:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionDuckOthers | AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers
error:nil];
很有魅力!
关于ios - 如何配置 AVAudioSession 来回避音乐并暂停语音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53092824/