Xcode Objective-C:即使 iPhone 设置为另一种语言,文字转语音始终为英语

Xcode Objective-C: Text to speech is always in English even if the iPhone is set to another language

我有这段代码可以让我的应用程序读取文本

self->myUtterance = [AVSpeechUtterance speechUtteranceWithString:@"Ciao come stai?"];
self->myUtterance.rate = 0.5;
self-> myUtterance.pitchMultiplier = 1.5;
//self-> myUtterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"it-IT"];
self->synth = [[AVSpeechSynthesizer alloc] init];
[self->synth speakUtterance:self->myUtterance];

我读过 "By default, AVSpeechSynthesizer will speak using a voice based on the user’s current language preferences." 但它不起作用。

事实上,即使我的 iPhone 全部设置为意大利语和地区,语音也是英语。

如果我去掉这行代码的注释

//self-> myUtterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"it-IT"];

那么语音是意大利语,但我希望语言是根据用户的语言偏好自动设置的。

您可以使用[[[NSBundle mainBundle] preferredLocalizations] firstObject]查找应用程序的当前语言。

(如果它是 nil 或者如果您的应用未本地化,您可以使用 [[NSLocale currentLocale] languageCode] 代替,但这不会处理设置中指定的语言,每当用户更改语言时仅适用于应用程序,不适用于所有设备)

这样您就可以使用 [AVSpeechSynthesisVoice voiceWithLanguage:] 指定语言,并确保您使用的是应用语言。

否则你可以让用户选择口语。