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