我使用
AVSpeechSynthesizer
创建了一个具有语音功能的应用程序,并在 TableView 中通过 AVSpeechSynthesisVoice: speechVoices()
功能显示语言列表。用户可以从 TableView 中选择语音语言。
我想要实现的下一件事是添加语音性别,以便用户可以在默认的Male或Female声音之间切换,就像在Siri应用程序中一样。我不想让它变得复杂,所以只是寻找默认的男性或女性声音。
从
speechVoices()
返回的语音具有特定于每个语音的默认男声或女声。
我可以改变它吗?我在应用程序设置页面上放置了
UIPickerView
来选择男声和女声。
但是,我做了一些研究,但找不到任何文档。似乎直到最新的 iOS 版本才提供此类支持。
到目前为止有人尝试过实现这样的功能吗?
我认为您没有这些信息,并且每个声音都是男性或女性,因此您必须对您正在使用的语言的所有可用声音进行分类,以便在运行时您可以使用此信息对它们进行排序。这是我非常愿意在我的应用程序上使用的东西。
//NSArray* voices = AVSpeechSynthesisVoice.speechVoices;
//voice identifier inspection reports
//com.apple.ttsbundle.Samantha-compact
//com.apple.speech.synthesis.voice
//AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:text];
//Replace hard-coded name with any available in the list pulled from the speechVoices array
NSUInteger idx = [AVSpeechSynthesisVoice.speechVoices indexOfObjectPassingTest:^BOOL(AVSpeechSynthesisVoice * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
return [obj.identifier containsString:@"Samantha"];
}];
if (idx > -1) {
AVSpeechSynthesisVoice* voice = AVSpeechSynthesisVoice.speechVoices[idx];
utterance.voice = voice;
}