我一直在努力学习 AVSpeechSynthesis。该代码在 iOS 模拟器中运行良好(在 iPad 和 iOS 上),但文本到语音功能在我的 iPad 上根本不起作用。这是我的代码。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//Load Model
self.string = @"Hello World";
self.utterance = [[AVSpeechUtterance alloc] initWithString:self.string];
self.utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
self.utterance.rate = (float) 0.25;
//Load UI
self.textLabel.text = self.string;
}
- (IBAction)startSpeaking:(id)sender {
self.speechSynthesizer = [[AVSpeechSynthesizer alloc] init];
if ([self.speechSynthesizer isSpeaking]) {
[self.speechSynthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryWord];
} else {
[self.speechSynthesizer speakUtterance:self.utterance];
}
}
更新: AVSpeechSyntheSizer 正在工作,但没有声音 所以我有一个 UILabel,其文本与 UI 上的 AVSpeechUtterance 相同。我实现了 AVSpeechSynthesizerDelegate,这样当我点击 startSpeaking: 时,文本标签会将当前单词变为红色。所以我知道 AVSpeechSynthesizer 必须工作,因为它正在进行委托调用,但我不知道为什么我听不到它。音量已调高,没有其他应用正在播放音频。