如何将AVAudioSession配置为回避音乐并暂停口语音频



我正在开发一个导航应用程序,该应用程序使用AVSpeechSynthesizer来呼叫方向。如果我在播放播客,我希望能够在说出方向时暂停音频,然后继续播放。这是使用成功实现的

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers
error:nil];

然而,如果我使用上面的代码播放音乐,指令会与音乐混合,而音乐的音量不会降低,这会使我很难听到指令。所以我尝试了:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionDuckOthers
error:nil]; 

现在背景音乐的音量降低了,这正是我想要的,但当我播放口语音频(即播客(时,它不再暂停,而是与指令混合在一起——尽管音量降低了。

我真正想要的是:AVAudioSessionCategoryOptionInterruptSpokenAudioAndDuckWithOthers。然而,这并不存在。我知道这种组合是可能的,因为其他应用程序(如Waze(也有这种行为。

我知道你可以检测音频何时播放使用:

BOOL isOtherAudioPlaying = [[AVAudioSession sharedInstance] isOtherAudioPlaying];

但是,我找不到一种方法来确定音频是否被说出。如果可以的话,我可以根据任何时候的比赛来安排。有人能帮忙吗,最好是在目标C中?

这就是它的实现方式:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionDuckOthers | AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers
error:nil];

真有魅力!

最新更新