文字转语音(TTS)iOS7

时间:2014-04-22 20:20:09

标签: ios text-to-speech avspeechsynthesizer universal-volume-control

我的应用程序想要在可用时播放一些文本,如果在后台播放一些音乐我想在我的应用播放文本时降低音乐的声音,我所做的是:

[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硬件音量控制在手机左侧的那个)。即我的应用音量不能高或低,除非我在改变音量时在我的应用中正在播放文字。

2 个答案:

答案 0 :(得分:2)

非常感谢,在我播放文字之前,如果我这样做了,那就有用了:

[[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];

答案 1 :(得分:0)

setActive:设置为NO后,立即将其设置为YES!将类别设置为Ambient,以便可以继续播放背景声音。

所以,在你玩之前:

[[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];