2014-07-26 125 views
0

我在我的应用程序中使用AVSpeechSynthesizer for TTS。这些是我设置iOS 7 TTS的步骤。AVSpeechSynthesizer不暂停

-(void)viewDidLoad 
{ 
    speechPaused = NO; 
    [self playSound]; 
} 
// The sound is playing fine. 
-(void) playSound { 
    speechPaused = NO; 
    AVSpeechUtterance* utter = [[AVSpeechUtterance alloc] initWithString:_text]; 
    utter.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"]; 
    [utter setRate:0.2f]; 
    if (!self.synthesizer) { 
     self.synthesizer = [AVSpeechSynthesizer new]; 
    } 
    self.synthesizer.delegate = self; 
    [self.synthesizer speakUtterance:utter]; 
} 
// This is what isn't working 
- (void) pauseSpeech { 
    if (speechPaused == NO) { 
     [self.synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate]; 
     NSLog(@"Pause"); 
     speechPaused = YES; 
    } 
    if (self.synthesizer.speaking == NO) { 
     AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:@""]; 
     utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-au"]; 
     [self.synthesizer speakUtterance:utterance]; 
    } 
} 
- (void) playSpeech { 
    [self.synthesizer continueSpeaking]; 
    speechPaused = NO; 
    NSLog(@"Play"); 
} 
- (IBAction)pauseSpeech:(id)sender { 
    [self pauseSpeech]; 
} 
- (IBAction)startSpeech:(id)sender { 
    [self playSpeech]; 
} 

我还设置了的NSLog这个方法,但它从来没有被称为:

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance 
{ 
    NSLog(@"speechSynthesizer didPauseSpeechUtterance"); 
} 

我一直停留在此的

#import <AVFoundation/AVFoundation.h> 

@interface SpeakViewController : UIViewController <AVSpeechSynthesizerDelegate> { 
    BOOL speechPaused; 
} 
@property (strong, nonatomic) AVSpeechSynthesizer *synthesizer; 
@end 
在我的.m

然后天。任何帮助表示赞赏。

+0

是'pauseSpeech'方法曾经叫什么名字? –

回答

0

尝试这个

-(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]; 
}