gpt4 book ai didi

ios - AVSpeechSynthesizer - 使用 AVSpeechBoundaryImmediate 时不会停止

转载 作者:行者123 更新时间:2023-11-29 12:39:12 25 4
gpt4 key购买 nike

我有两个 View Controller 。一和二。

  1. 在 oneViewController 上,有一个使用 Storyboard 调用 TwoViewController 的按钮 - 工作正常
  2. 在 TwoViewController 上有一个按钮可以使用按钮(Listen Button)读取一些文本 - 工作正常
  3. TwoViewController 上有返回按钮,可将您带回到 oneViewController。

问题:

如果两次点击 Listen 按钮并按下 Back 按钮, View Controller 从 Two 变为 One,但语音仍在后台进行。有人可以帮忙停止演讲吗?

TwoViewController 上的代码:

AVSpeechSynthesizer *合成器;

-(IBAction) Back:(id)sender
{
[_synth stopSpeakingAtBoundary: AVSpeechBoundaryImmediate]; --not working
[self dismissViewControllerAnimated:YES completion:nil]; - working fine.
}

-(void) viewDidDisappear:(BOOL)animated
{
[_synth stopSpeakingAtBoundary:AVSpeechBoundaryImmediate]; --not working
[self dismissViewControllerAnimated:YES completion:nil];
}

-(IBAction)listenButton:(id)sender
{
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"I am running a test."];
[utterance setRate:0.18f];
_synth=[[AVSpeechSynthesizer alloc] init];
[_synth speakUtterance:utterance];

}

注意:如果单击“收听”按钮然后单击“返回”按钮,则不会发生此问题。仅当多次单击“收听”按钮然后单击“返回”按钮时才会出现问题。

最佳答案

尝试暂停、恢复和停止选项

-(void)stopSpeechReading
{
if([synthesize isSpeaking]) {
NSLog(@"Reading has been stopped");
[synthesize stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@""];
[synthesize speakUtterance:utterance];
[synthesize stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
}

-(void)pauseSpeechReading
{
if([synthesize isSpeaking]) {
NSLog(@"Reading paused");
[synthesize pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@""];
[synthesize speakUtterance:utterance];
[synthesize pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
}

-(void)resumeSpeechReading
{
NSLog(@"Reading resumed");
[synthesize continueSpeaking];
}

关于ios - AVSpeechSynthesizer - 使用 AVSpeechBoundaryImmediate 时不会停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25457297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com