我使用以下代码使用按钮初始化文本到语音合成。但有时用户可能希望在讲话中途停止语音。我可以知道是否有任何代码可以做到这一点。
谢谢
这是我的代码
@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
最佳答案
But sometimes users may want to stop the voice in the middle of the speech.
要停止语音,请向语音合成器发送 -stopSpeakingAtBoundary:
留言:
[self.speechSynthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
如果您希望语音继续到当前单词的末尾而不是立即停止,请使用 AVSpeechBoundaryWord
而不是 AVSpeechBoundaryImmediate
。
您也可以pause speech而不是完全停止它。
关于ios - 如何在文本到语音合成过程中停止语音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21240215/