ios - 如何在文本到语音合成过程中停止语音?

标签 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

最佳答案

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/

相关文章:

ios - UIActivityController 在 Facebook 上分享时不报告文本和图像

ios - 为什么不允许我的团队管理 iCloud 容器?

iphone - 如何在 Objective-C 中制作一个向上计数的计时器?

ios - 更改导航栏的色调颜色 PKAddPassesViewController

ios - 如何将手势识别器添加到 UILabel?

iphone - 如何根据html内容设置UIWebView的高度?

iphone - 在 IOS 中以编程方式创建 View (它是如何工作的)?

iphone - "No provisioning iOS device connected"

objective-c - NSString指针: theoretical questions after study

swift - 需要帮助将 (CFPropertyListRef *)nsdictionary 转换为 swift