我的应用程序想要在文本可用时播放一些文本,如果有一些音乐在后台播放我想在我的应用程序播放其文本时降低音乐的声音,我所做的是:
[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];
所以音乐会恢复到原来的音量。
问题是将 session 事件设置为否,我失去了音量控制(iPhone 硬件音量控制在手机左侧)。也就是说,除非在我更改音量时正在我的应用程序中播放文本,否则我无法调高或调低我的应用程序的音量。
最佳答案
非常感谢,如果我在播放文本之前这样做,它会起作用:
[[AVAudioSession sharedInstance] setActive:NO withOptions:0 error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionDuckOthers
error:nil];
我必须 setActive:NO
因为没有它,当我第二次播放文本时,音乐将暂停!!
然后在我播放我的文字后,我这样做:
[[AVAudioSession sharedInstance] setActive:NO withOptions:0 error:nil];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient withOptions: 0 error: nil];
[[AVAudioSession sharedInstance] setActive:YES withOptions: 0 error:nil];
关于ios - 文字转语音 (TTS) iOS7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23229416/