如何在文本到语音合成期间停​​止声音?

时间:2014-01-20 17:18:51

标签: ios iphone objective-c ios7 text-to-speech

我使用以下代码使用按钮初始化文本到语音合成。但有时用户可能希望在演讲中停止发声。我可以知道有没有我可以做的代码。

谢谢

这是我的代码

@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

2 个答案:

答案 0 :(得分:6)

  

但有时用户可能希望在演讲中停止发声。

要停止语音,请向语音合成器发送-stopSpeakingAtBoundary:消息:

[self.speechSynthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];

如果您希望语音继续到当前单词的结尾而不是立即停止,请使用AVSpeechBoundaryWord代替AVSpeechBoundaryImmediate

你也可以pause speech而不是完全停止它。

答案 1 :(得分:-1)

这将完全停止SpeechSynthesizer:-stopSpeakingAtBoundary,如果你想在SS之后需要更多代码,那么将:放在最后。基本上,-stopSpeakingAtBoundary:要获得适合您的完整代码,请访问:[self.speechSynthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];