[英]Text To Speech (TTS) iOS7
我的应用程序想要在文本可用时播放一些文本,如果在后台播放音乐,我想在应用程序播放文本时降低音乐的声音,我要做的是:
[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硬件的音量控件位于手机的左侧)。 也就是说,除非更改音量的那一刻在我的应用程序中正在播放文本,否则我无法提高或降低应用程序的音量。
非常感谢您,如果我这样做,在我播放文字之前会奏效:
[[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];
将setActive:
设置为NO后,立即将其再次设置为YES
! 将类别设置为环境,以便可以继续播放背景声音。
所以,在您玩之前:
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient
withOptions: AVAudioSessionCategoryOptionDuckOthers
error: nil];
[[AVAudioSession sharedInstance] setActive:YES withOptions: 0 error:nil];
播放后:
[[AVAudioSession sharedInstance] setActive:NO withOptions:0 error:nil];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient
withOptions: 0
error: nil];
[[AVAudioSession sharedInstance] setActive:YES withOptions: 0 error:nil];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.