Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to change voice using AVSpeechSynthesis in swift

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 717
    Comment on it

    This blog includes the use of -

    AVSpeechSynthesisVoice

     If we want to change the default voice i.e system voice then we need to use speechVoices{} array which contains the name of persons and locations. iOS has a nice method for setting the voice but there is only support for a single identifier, the Alex voice.

    Here is the Code-

    let voice = AVSpeechSynthesisVoice(identifier: AVSpeechSynthesisVoiceIdentifierAlex)

    To use another voice, the array of voices can be searched for a matching name. The identifier in the previous call does not match the name of a voice as you might imagine it to.

    var voiceToUse: AVSpeechSynthesisVoice?
    for voice in AVSpeechSynthesisVoice.speechVoices() {
        if #available(iOS 9.0, *) {
    
             // use the name that you want to use from array. e.g.- "Karen"
            if voice.name == "Karen" {
            voiceToUse = voice
    
            }
        } 
    } 

    To use voice, assign the voice using the voice property on an utterance and then speak the utterance.

    let utterance = AVSpeechUtterance(string: "Hello from iOS.")
    utterance.voice = voiceToUse
    let synth = AVSpeechSynthesizer()
    synth.speakUtterance(utterance)

    Also we can set the pitch and rate of voice by using properties of AVSpeechUtterance.Find the Code here-

    //set the rate of voice
    utterance.rate = 0.5

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: