iOS-AVSpeechSynthesizer語音合成

iOS中的AVSpeechSynthesizer可以很輕松的實現實現文本到語音的功能,基本代碼如下:

    self.speechSynthesizer = [[AVSpeechSynthesizer alloc] init];
    AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"FlyElephant"];
    
    AVSpeechSynthesisVoice *voiceType = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
    utterance.voice = voiceType;
    //設置語速
    utterance.rate *= 0.5;
    //設置音量
    utterance.volume = 0.6;

    [self.speechSynthesizer speakUtterance:utterance];

AVSpeechUtterance可以設置對應的語言,如果設置的語言不能識別文本不能生成語音播放,蘋果支持的語言如下:

  • Arabic (ar-SA)
  • Chinese (zh-CN, zh-HK, zh-TW)
  • Czech (cs-CZ)
  • Danish (da-DK)
  • Dutch (nl-BE, nl-NL)
  • English (en-AU, en-GB, en-IE, en-US, en-ZA)
  • Finnish (fi-FI)
  • French (fr-CA, fr-FR)
  • German (de-DE)
  • Greek (el-GR)
  • Hebrew (he-IL)
  • Hindi (hi-IN)
  • Hungarian (hu-HU)
  • Indonesian (id-ID)
  • Italian (it-IT)
  • Japanese (ja-JP)
  • Korean (ko-KR)
  • Norwegian (no-NO)
  • Polish (pl-PL)
  • Portuguese (pt-BR, pt-PT)
  • Romanian (ro-RO)
  • Russian (ru-RU)
  • Slovak (sk-SK)
  • Spanish (es-ES, es-MX)
  • Swedish (sv-SE)
  • Thai (th-TH)
  • Turkish (tr-TR)

以上就是蘋果支持的語言編碼,當然你也可以通過speechVoices遍歷對應的語言編碼:

    NSArray *voice = [AVSpeechSynthesisVoice speechVoices];
    for (AVSpeechSynthesisVoice *voiceModel in voice) {
        NSLog(@"FlyElephant-%@",voiceModel);
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容