Swift-如何将文本作为描述性字符串将文本作为语音语言



我正在尝试将文本转换为语音语言代码(在BCP 47中(,以转换为描述性字符串,以选择该语言在应用程序中具有文本到语音功能。p>预期 -

阿拉伯语(沙特阿拉伯(-AR -SA中国(中国(-ZH -CN中国(香港中国(-ZH -HK中文(台湾(-ZH -TW捷克(捷克共和国(-CS -CZ

实际 -

ar-sa(固定(CS-CZ(固定(

class func getListOfSupportedLanguages() {
        for voice in (AVSpeechSynthesisVoice.speechVoices()){
            print(voice.language)
            let language = Locale.init(identifier: voice.language)
            print(language.description)
        }
    }

我正在关注这个有用的网站并获得不同的结果。

https://useyourloaf.com/blog/synthesized-speech-from-text/

@property (strong, nonatomic) NSArray *languageCodes;
@property (strong, nonatomic) NSDictionary *languageDictionary;
@property (strong, nonatomic) AVSpeechSynthesizer *synthesizer;
- (NSArray *)languageCodes
{
    if (!_languageCodes)
    {
        _languageCodes = [self.languageDictionary keysSortedByValueUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    }
    return _languageCodes;
}
// Map between language codes and locale specific display name
- (NSDictionary *)languageDictionary
{
    if (!_languageDictionary)
    {
        NSArray *voices = [AVSpeechSynthesisVoice speechVoices];
        NSArray *languages = [voices valueForKey:@"language"];
        NSLocale *currentLocale = [NSLocale autoupdatingCurrentLocale];
        NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
        for (NSString *code in languages)
        {
            dictionary[code] = [currentLocale displayNameForKey:NSLocaleIdentifier value:code];
        }
        _languageDictionary = dictionary;
    }
    return _languageDictionary;
}

我使它起作用,这要归功于Matt -NSlocale具有显示名称,而语言环境没有一个。根据下面的文章

nslocale swift 3

这是代码 -

   class func getListOfSupportedLanguages() {
         let current = Locale.current.languageCode
         for voice in (AVSpeechSynthesisVoice.speechVoices()){
              let language = NSLocale.init(localeIdentifier: current!)
              print(language.displayName(forKey: NSLocale.Key.identifier, value: voice.language)?.description as! String)
         }
   }

输出 -

English (Australia)
English (United Kingdom)
English (Ireland)
English (United States)
English (South Africa)

要做的第一件事是确保将AVFoundation库导入将实现AVSpeechSynthesizerAVSpeechSynthesizer及其属性和方法的标题(.h(文件。<<<<<<<<<<<<<<<<<<<<<<<<<<

ex: #import <AVFoundation/AVFoundation.h>

这篇文章中的出色反馈。谢谢大家。

相关内容

  • 没有找到相关文章

最新更新