gpt4 book ai didi

ios - AVSpeechSynthesizer 没有停止

转载 作者:可可西里 更新时间:2023-11-01 05:08:43 24 4
gpt4 key购买 nike

我在我的 iOS 应用程序中使用 AVSpeechSynthesizer for TTS。但是没有调用 didFinishSpeechUtterance 方法。

这是我在 .h 中的代码:

#import <AVFoundation/AVFoundation.h>

@interface EmailViewController : UIViewController <UITextFieldDelegate, AVSpeechSynthesizerDelegate>

@property (nonatomic, strong) AVSpeechSynthesizer* talker;

这是在 .m 中

- (void) startTalking {
NSString *hi = [NSString stringWithFormat:@"%@", humanLanguageArray[speaknumber]];
AVSpeechUtterance* utter = [[AVSpeechUtterance alloc] initWithString:hi];
utter.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
[utter setRate:0.2f];
if (!self.talker) {
self.talker = [AVSpeechSynthesizer new];
}
self.talker.delegate = self;
[self.talker speakUtterance:utter];
}
- (void)stopSpeech
{
AVSpeechSynthesizer *talked = [[AVSpeechSynthesizer alloc] init];
if([talked isSpeaking]) {
[talked stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@""];
[talked speakUtterance:utterance];
[talked stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utteranc
{
if (speaknumber < (length-1)) {
speaknumber += 1;
self.talker.delegate = self;
NSString *hi = [NSString stringWithFormat:@"%@", humanLanguageArray[speaknumber]];
AVSpeechUtterance *utter = [[AVSpeechUtterance alloc] initWithString:hi];
utter.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
[utter setRate:0.2f];
if (!self.talker) {
self.talker = [AVSpeechSynthesizer new];
}
[self.talker speakUtterance:utter];
} else if (speaknumber >= length) {
}
}

如果我调用 stopSpeech,它不会停止说话,它会一直说话直到 speaknumber >= length。有谁知道如何解决这个问题?

最佳答案

为什么要在stopSpeech中再次实例化AVSpeechSynthesizer?

- (void)stopSpeech
{
AVSpeechSynthesizer *talked = self.talker;
if([talked isSpeaking]) {
[talked stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@""];
[talked speakUtterance:utterance];
[talked stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
}

说话的实例是 self.talker,不是吗?

关于ios - AVSpeechSynthesizer 没有停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24687719/

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