2014-01-20 81 views
1

我用下面的代码将文本初始化为使用按钮进行语音合成。但有时用户可能想要在演讲中停止发言。我可否知道是否有任何代码可以做到这一点。如何在文本到语音合成期间停​​止语音?

感谢

这里是我的代码

@interface RMDemoStepViewController() 

@end 

@implementation RMDemoStepViewController 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    //Add Border to TextBox 


    //Instantiate the object that will allow us to use text to speech 
    self.speechSynthesizer = [[AVSpeechSynthesizer alloc] init]; 
    [self.speechSynthesizer setDelegate:self]; 

} 
- (IBAction)speakButtonWasPressed:(id)sender{ 

    [self speakText:[self.textView text]]; 

} 

- (void)speakText:(NSString *)toBeSpoken{ 

    AVSpeechUtterance *utt = [AVSpeechUtterance speechUtteranceWithString:toBeSpoken]; 
    utt.rate = [self.speedSlider value]; 
    [self.speechSynthesizer speakUtterance:utt]; 

} 

- (IBAction)speechSpeedShouldChange:(id)sender 
{ 
    UISlider *slider = (UISlider *)sender; 
    NSInteger val = lround(slider.value); 
    NSLog(@"%@",[NSString stringWithFormat:@"%ld",(long)val]); 
} 
@end 

回答

6

但有时用户可能要停止的声音在演讲中。

要停止讲话,发送语音合成一个-stopSpeakingAtBoundary:消息:如果你想讲话继续到当前单词的末尾,而不是停止瞬间

[self.speechSynthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate]; 

使用AVSpeechBoundaryWord,而不是AVSpeechBoundaryImmediate。您也可以pause speech而不是完全停止它。

-1

这将完全停止SpeechSynthesizer:-stopSpeakingAtBoundary,并且如果您想在SS之后添加更多代码,请在末尾放置:。所以基本上,-stopSpeakingAtBoundary:要完整的代码,以适应你的,在这里:[self.speechSynthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];