我有以下代码来说出一个简单的文本。在iOS上,代码工作得很好,文本也可以说出来。但在Mac应用程序上,它不会发出声音。
//Setting up text to speech
synthesizer = [[AVSpeechSynthesizer alloc]init];
synthesizer.delegate = self;
if (synthesizer) {
if ([synthesizer isSpeaking]) {
[synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];;
}
}
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:text];
[utterance setRate:0.5f];
utterance.postUtteranceDelay = 0;
//utterance.voice = [AVSpeechSynthesisVoice voiceWithIdentifier:AVSpeechSynthesisVoiceIdentifierAlex];
dispatch_async(dispatch_get_main_queue(), ^{
[self->synthesizer speakUtterance:utterance];
});
在iOS上触发以下委托方法
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance{
}
但在苹果应用程序上,它会直接转到didCancelSpeechUtterance
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance{
}
我不知道如何找到导致问题的错误。在这方面的任何帮助都将不胜感激。
发布于 2021-08-16 13:30:04
此问题已在指明语言后修复
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en"];
在iOS中,它在不指定语言的情况下工作得很好。但在Mac catalyst上,我想这是必需的。
https://stackoverflow.com/questions/68803394
复制相似问题