iOS提供了一个类AVSpeechSynthesizer来实现文本到语音的功能, 即读出文字
直接上代码:
AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init]; NSArray *speechStrings = @[@"Hello AV Foundation. How are you?", @"我很好, 感谢!", @"Are you excited about the book?", @"是的, 我非常喜欢这本书.", @"What‘s your favourite feature?", @"我都很喜欢呢!", @"It was great to speak with you!", @"我也是, 祝你愉快"]; NSArray *voices = @[[AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"], [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-cn"]]; for (int i = 0; i < speechStrings.count; i ++) { AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:speechStrings[i]]; utterance.voice = voices[i % 2]; // 语言 utterance.rate = 0.4f; // 播放语音的速度 utterance.pitchMultiplier = 0.8f; // 音调 utterance.postUtteranceDelay = 0.1f; // 下一句间隔时间 // 发音 [synthesizer speakUtterance:utterance]; }
时间: 2024-10-06 00:16:39