【问题标题】:How to stop the voice during text-to-speech synthesis?如何在文本到语音合成过程中停止声音?
【发布时间】:2014-02-09 23:45:04
【问题描述】:

我使用以下代码使用按钮初始化文本到语音合成。但有时用户可能希望在语音中间停止声音。我可以知道是否有任何代码可以做到这一点。

谢谢

这是我的代码

@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

【问题讨论】:

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


    【解决方案1】:

    但有时用户可能希望在讲话中间停止声音。

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

    [self.speechSynthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
    

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

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

    【讨论】:

      【解决方案2】:

      这将完全停止 SpeechSynthesizer:-stopSpeakingAtBoundary,如果您想在 SS 之后添加更多代码,请在末尾添加 :。所以基本上,-stopSpeakingAtBoundary: 要获得适合您的完整代码,请点击此处:[self.speechSynthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];

      【讨论】:

        猜你喜欢
        • 2012-03-09
        • 2017-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-03
        • 2010-12-17
        • 1970-01-01
        相关资源
        最近更新 更多