我的应用程序想要在可用时播放一些文本,如果在后台播放一些音乐,我想在我的应用程序播放文本时降低音乐的声音,我所做的是:
[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionDuckOthers
error:&err];
[[AVAudioSession sharedInstance] setActive:YES error:&err];
该选项AVAudioSessionCategoryOptionDuckOthers
将在我的应用程序播放其文本时降低音乐音量。然后用 , 播放文本,然后speechSynthesizer
在:
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)
我做的话语:
[[AVAudioSession sharedInstance] setActive:NO withFlags:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
所以音乐将恢复到原来的音量。
通过将会话活动设置为 NO 的问题,我失去了音量控制(iPhone 硬件音量控制在手机左侧)。即,我无法提高或降低我的应用程序的音量,除非在我更改音量时我的应用程序中正在播放文本。