Xcode Objective-C:即使iPhone设置为其他语言,文本到语音也始终是英语

问题描述 投票:0回答:1

我有此代码可让我的应用读取文本

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];

我已经读过“默认情况下,AVSpeechSynthesizer将根据用户当前的语言偏好使用语音进行说话。”但它不起作用。

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

如果我删除对这一行代码的注释

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

然后声音是意大利语,但我希望根据用户的语言首选项自动设置语言。

ios objective-c text-to-speech
1个回答
0
投票

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

((如果是nil,则可以改用[[NSLocale currentLocale] languageCode],但这不会照顾设置中指定的语言,只要用户仅更改应用程序的语言,而不能更改所有设备的语言)

通过这种方式,您可以使用[AVSpeechSynthesisVoice voiceWithLanguage:]指定语言,并确保您说出了应用语言。

© www.soinside.com 2019 - 2024. All rights reserved.