Home > Software design >  Text to speech from notification in ios swift
Text to speech from notification in ios swift

Time:08-01

I develop an app in ios swift. The data come from push notifications. In the notification payload, there is a text which is for playing. I play text to speech through AVSpeechSynthesizer but I want to play it 2,3 times it only plays one time. Please help me to resolve this issue. Thanks in advance.Here is the code

func textToSpeechWithMessage(message:String, _ languageCode:String)
    {
        
        let synthesizer = AVSpeechSynthesizer()
        
        let audioSession = AVAudioSession.sharedInstance()
        
        print("Activating audio session")
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)
            
            try audioSession.setActive(true)
            UIApplication.shared.beginReceivingRemoteControlEvents()
            
            let utterance = AVSpeechUtterance(string:message)
            utterance.rate = 0.5
            utterance.volume = 0.8
            utterance.voice = AVSpeechSynthesisVoice(language: languageCode)
            synthesizer.speak(utterance)

            
        } catch {
            print("Unable to set audio session category: %@", error)
        }
    }

CodePudding user response:

To achieve that, you need to:

  1. Conform your class to AVSpeechSynthesizerDelegate protocol.
  2. Implement the speechSynthesizer(_:didFinish:) method.
  3. Repeat the speech after the last one has finished as many times as you wish.

Something like this:

    private let synthesizer = AVSpeechSynthesizer()
    private let audioSession = AVAudioSession.sharedInstance()
    private var repeatCount = 3
    private var currentCount = 0
    private var message = ""
    private var languageCode = ""

    func repeatTextToSpeach(with message: String, languageCode: String, repeatCount: Int) {
        self.synthesizer.delegate = self
        self.message = message
        self.repeatCount = repeatCount
        self.languageCode = languageCode
        self.currentCount = 0
        self.speak()
    }

    func speak() {
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)
            try audioSession.setActive(true)

            UIApplication.shared.beginReceivingRemoteControlEvents()
            
            let utterance = AVSpeechUtterance(string: message)
            utterance.rate = 0.5
            utterance.volume = 0.8
            utterance.voice = AVSpeechSynthesisVoice(language: languageCode)

            synthesizer.speak(utterance)
            currentCount  = 1
        } catch {
            print("Unable to set audio session category: %@", error)
        }
    }
    
    func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
        if currentCount < repeatCount {
            speak()
        }
    }

and start the repeated speech like this:

repeatTextToSpeach(with: "Text to speech from notification in ios swift", languageCode: "en-US", repeatCount: 3)
  • Related