2013-10-14 118 views
2

我創建了一個AVSpeechSynthesizer並開始播放AVSpeechUtterance。這工作正常。如果用戶按下按鈕,則暫停合成器。這也適用。但是,當我嘗試用continueSpeaking方法重啓合成器時,沒有任何反應。如果我檢查isSpeaking財產,它仍然是NO。我怎樣才能讓音頻從停止播放的地方開始播放?AVSpeechSynthesizer繼續說不工作

AVSpeechSynthesizer *synthesizer_; 

synthesizer_ = [[AVSpeechSynthesizer alloc] init]; 
synthesizer_.delegate = self; 


- (void)textToSpeech{ 
    AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc]initWithString:itemText]; 
    utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:localeCode]; 
    utterance.rate = UTTERANCE_RATE; 
    utterance.preUtteranceDelay = itemDelayTimeInterval; 
    [synthesizer_ speakUtterance:utterance]; 
} 

- (IBAction)pauseButtonPressed:(id)sender { 
    if (synthesizer_.isSpeaking) { 
     [synthesizer_ pauseSpeakingAtBoundary:AVSpeechBoundaryWord]; 
    } 
    else{ 
     [synthesizer_ continueSpeaking]; 
    } 

}

+0

我有一個類似的問題,其中'[synthesizer_pauseSpeakingAtBoundary:AVSpeechBoundaryWord]'不會暫停說話。相反,我得到多個'didPauseSpeechUtterance' /'didContinueSpeechUtterance'事件,直到話語完成。使用'pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate'似乎更好。 –

+0

我將代碼中的所有其他引用刪除到AVAudio項目,然後我經歷了與重做'didPauseSpeechUtterance' /'didContinueSpeechUtterance'事件相同的事情。 – user2880019

回答

1

你原來的代碼是:

if (synthesizer_.isSpeaking) { 

試試這個:

if (![synthesizer_ isPaused]) 

原因:

從文檔: 來講 一個布爾值,指示合成器是否正在講話。 (只讀)

@property(nonatomic, readonly, getter=isSpeaking) BOOL speaking 

討論如果合成器說話或有排隊說話,哪怕是目前暫停話語 返回YES。如果合成器已經完成了在其隊列中說出所有話語,或者如果還沒有給出說話的話語,則返回NO

可用性 適用於iOS 7.0或更高版本。 宣佈 AVSpeechSynthesis.h

所以,如果你使用這個屬性,和語音暫停時,該屬性可真和你繼續發言的代碼將無法運行。

0

嘗試使用如下代碼

-(void)stopSpeechReading 
{ 
    if([synthesize isSpeaking]) { 
     NSLog(@"Reading has been stopped"); 
     [synthesize stopSpeakingAtBoundary:AVSpeechBoundaryImmediate]; 
     AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@""]; 
     [synthesize speakUtterance:utterance]; 
     [synthesize stopSpeakingAtBoundary:AVSpeechBoundaryImmediate]; 
    } 
} 
-(void)pauseSpeechReading 
{ 
    if([synthesize isSpeaking]) { 
     NSLog(@"Reading paused"); 
     [synthesize pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate]; 
     AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@""]; 
     [synthesize speakUtterance:utterance]; 
     [synthesize pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate]; 
    } 
} 
-(void)resumeSpeechReading 
{ 
    NSLog(@"Reading resumed"); 
    [synthesize continueSpeaking]; 
} 
0

只是isPaused得到啓動,它會正常工作:

注:使用AVSpeechBoundaryImmediate代替AVSpeechBoundaryWord爭取更好的成績。