AVSpeechSynthesizer in SwiftUI

An object that produces synthesized speech from text utterances and enables monitoring or controlling of ongoing speech.

To speak some text, create an AVSpeechUtterance instance that contains the text and pass it to speak(_:) on a speech synthesizer instance. You can optionally also retrieve an AVSpeechSynthesisVoice and set it on the utterance’s voice property to have the speech synthesizer use that voice when speaking the utterance’s text.

The speech synthesizer maintains a queue of utterances that it speaks. If the synthesizer isn’t speaking, calling speak(_:) begins speaking that utterance either immediately or after pausing for its preUtteranceDelay, if necessary. If the synthesizer is speaking, the synthesizer adds utterances to a queue and speaks them in the order it receives them.

After speech begins, you can use the synthesizer object to pause or stop speech. After pausing, you can resume the speech from its paused point or stop the speech entirely and remove all remaining utterances in the queue.

You can monitor the speech synthesizer by examining its isSpeaking and isPaused properties, or by setting a delegate that conforms to AVSpeechSynthesizerDelegate. The delegate receives significant events as they occur during speech synthesis.

An AVSpeechSynthesizer also controls the route where the speech plays. See Directing Speech Output for more information.

//
//  ViewController.swift
//  SpechSynthesizerDemo
//
//  Created by Joynal Abedin on 22/2/23.
//

import UIKit
import AVFoundation

class ViewController: UIViewController {
    
    let speaker = AVSpeechSynthesizer()
    let dialogue = AVSpeechUtterance(string: "Hello I am clearly a man")
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        speakText()
    }
    
    func speakText() {
        dialogue.rate = AVSpeechUtteranceDefaultSpeechRate
        dialogue.voice = AVSpeechSynthesisVoice(language: Voice.Moira.person)
        
        //        dialogue.rate = 0.4
        //        dialogue.pitchMultiplier = 0.5
        //        dialogue.preUtteranceDelay = 0
        //        dialogue.volume = 1
        print(AVSpeechSynthesisVoice.speechVoices())
        
        guard speaker.isSpeaking else {
            speaker.speak(dialogue)
            return
        }
    }
    
    
}

enum Voice {
    case Maged, Zuzana, Sara, Anna, Melina, Karen, Daniel, Moira, Samantha, Tessa, Monica, Paulina, Satu, Amelie, Thomas, Carmit,  Lekha, Mariska, Damayanti, Alice, Kyoko, Yuna, Ellen, Xander, Nora, Zosia, Luciana, Joana, Ioana, Milena, Laura, Alva, Kanya, Yelda, Ting_Ting, Sin_Ji, Mei_Jia
    
    var person: String {
        switch self {
        case .Maged:
            return "ar-SA"
        case .Zuzana:
            return "cs-CZ"
        case .Sara:
            return "da-DK"
        case .Anna:
            return "de-DE"
        case .Melina:
            return "el-GR"
        case .Karen:
            return "en-AU"
        case .Daniel:
            return "en-GB"
        case .Moira:
            return "en-IE"
        case .Samantha:
            return "en-US"
        case .Tessa:
            return "en-ZA"
        case .Monica:
            return "es-ES"
        case .Paulina:
            return "es-MX"
        case .Satu:
            return "fi-FI"
        case .Amelie:
            return "fr-CA"
        case .Thomas:
            return "fr-FR"
        case .Carmit:
            return "he-IL"
        case .Lekha:
            return "hi-IN"
        case .Mariska:
            return "hu-HU"
        case .Damayanti:
            return "id-ID"
        case .Alice:
            return "it-IT"
        case .Kyoko:
            return "ja-JP"
        case .Yuna:
            return "ko-KR"
        case .Ellen:
            return "nl-BE"
        case .Xander:
            return "nl-NL"
        case .Nora:
            return "no-NO"
        case .Zosia:
            return "pl-PL"
        case .Luciana:
            return "pt-BR"
        case .Joana:
            return "pt-PT"
        case .Ioana:
            return "ro-RO"
        case .Milena:
            return "ru-RU"
        case .Laura:
            return "sk-SK"
        case .Alva:
            return "sv-SE"
        case .Kanya:
            return "th-TH"
        case .Yelda:
            return "tr-TR"
        case .Ting_Ting:
            return "zh-CN"
        case .Sin_Ji:
            return "zh-HK"
        case .Mei_Jia:
            return "zh-TW"
        }
    }
}

Github source code: https://github.com/Joynal279/VoiceChangeDemo

416 thoughts on “AVSpeechSynthesizer in SwiftUI

Leave a Reply

Your email address will not be published. Required fields are marked *