简体   繁体   English

文字转语音(TTS)iOS7

[英]Text To Speech (TTS) iOS7

My application wants to play some text when it is available, and if there is some music playing in the background I want to lower the voice for the music while my app is playing its text, what I did is: 我的应用程序想要在文本可用时播放一些文本,如果在后台播放音乐,我想在应用程序播放文本时降低音乐的声音,我要做的是:

[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
                                 withOptions:AVAudioSessionCategoryOptionDuckOthers
                                      error:&err];
[[AVAudioSession sharedInstance] setActive:YES error:&err];

The option AVAudioSessionCategoryOptionDuckOthers will lower the music volume while my app is playing its text. 我的应用播放文本时,选项AVAudioSessionCategoryOptionDuckOthers会降低音乐音量。 Then play the text with the speechSynthesizer , after that in: 然后在以下语言中使用speechSynthesizer播放文本:

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)

utterance I do: 我所做的话:

[[AVAudioSession sharedInstance] setActive:NO withFlags:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];

so the music will be back to it original volume. 因此音乐将恢复到原始音量。

The problem by setting the session active to NO, I am loosing the volume control (the iPhone hardware volume control the one in the left side of the phone). 通过将会话活动设置为NO来解决问题,我正在松开音量控件(iPhone硬件的音量控件位于手机的左侧)。 ie I can not upper or lower the volume for my app unless there is a text actively playing in my app at the moment I am changing the volume. 也就是说,除非更改音量的那一刻在我的应用程序中正在播放文本,否则我无法提高或降低应用程序的音量。

Thank you very much, It works if I did this, before I play my text: 非常感谢您,如果我这样做,在我播放文字之前会奏效:

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

I had to setActive:NO as without it the second time when I play my text the music will be paused!! 我必须设置setActive:NO因为第二次播放文本时没有它,音乐将暂停!

And then After I play my text, I do this: 然后,在播放文字后,执行以下操作:

[[AVAudioSession sharedInstance] setActive:NO withOptions:0 error:nil];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient withOptions: 0 error: nil];
[[AVAudioSession sharedInstance] setActive:YES withOptions: 0 error:nil];

Immediately after setting setActive: to NO, set it to YES again! setActive:设置为NO后,立即将其再次设置为YES Set the category to Ambient so the background sound can continue playing. 将类别设置为环境,以便可以继续播放背景声音。

So, before you play: 所以,在您玩之前:

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient
                                 withOptions: AVAudioSessionCategoryOptionDuckOthers
                                       error: nil];
[[AVAudioSession sharedInstance] setActive:YES withOptions: 0 error:nil];

After you play: 播放后:

[[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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM