AVSpeechSynthesizer声音不变

时间:2020-01-03 05:52:07

标签: ios swift avspeechsynthesizer

我正在使用**AVSpeechSynthesizer**,以下是将文本传递到**AVSpeechSynthesizer**的代码。

func speakOutText(_ textToRead:String ){
    let audioSession = AVAudioSession.sharedInstance()
    do {
        try audioSession.setCategory(AVAudioSessionCategoryPlayback)
        try audioSession.setMode(AVAudioSessionModeDefault)
        try audioSession.setActive(true, with: .notifyOthersOnDeactivation)
    } catch {
        print("audioSession properties weren't set because of an error.")
    }
    let speakUtterance = AVSpeechUtterance.init(string: textToRead);
    speakUtterance.voice  = AVSpeechSynthesisVoice(language: "en-IN")
    speakUtterance.pitchMultiplier = 1.2
    speakUtterance.rate   = 0.5
    speechSynthesizer.speak(speakUtterance)

}

它正以女性声音正确说出文字。

但是,如果我在设置-> Siri&搜索-> Siri Voice->性别(男性)中更改了男性语音。再次调用speakOutText方法,它仅以女性语音而不是男性语音朗读文本。

有人可以让我知道原因,为什么它没有变成男性声音

2 个答案:

答案 0 :(得分:0)

使用此

  AVSpeechSynthesisVoiceGender.male

如果有任何疑问,请遵循appleLink

答案 1 :(得分:0)

private let synthesizer = AVSpeechSynthesizer()

var Rate: Float = AVSpeechUtteranceDefaultSpeechRate
var USMaleVoice = AVSpeechSynthesisVoice(language: "en-US")

func Say(_ phrase: String)
{
    let Utterance = AVSpeechUtterance(string: phrase)

    Utterance.rate = Rate

    Utterance.voice = AVSpeechSynthesisVoice(language: "en-US")

    synthesizer.speak(Utterance)
}
 //Print all voices ↓  
func GetVoices()
   {
    AVSpeechSynthesisVoice.speechVoices().forEach({ print($0)})
   }
相关问题