iosswifttext-to-speechavspeechsynthesizeravspeechutterance

SpeechSynthesizer doesn't give the natural pause for full-stop in Swift


I am working on a text to speak task. In which I am facing problem that if I got the text for example: Hello everyone. 2 minutes to go. Then using code below for speech synthesizer, It considers whole text as one sentence and doesn't give a natural pause after Hello everyone.. It speaks whole text as one sentence just like there is no . (full stop) after Hello everyone. This happens only if there is a number after punctuation mark . (full stop)

let utterance = AVSpeechUtterance(string: "Hello everyone. 2 minutes to go.")
utterance.voice = AVSpeechSynthesisVoice(language: "en-US")
utterance.rate = AVSpeechUtteranceDefaultSpeechRate
synthesizer.speak(utterance)

If you are concerned how I'm initializing session and synthesizer then here is the code below:

let synthesizer = AVSpeechSynthesizer()
let session = AVAudioSession.sharedInstance()
do {
    try session.setCategory(.playback, options: .duckOthers)
    try session.setActive(true)
} catch {
    print(error.localizedDescription)
}

Solution

  • @Umair This not just happens when you write something that starts with a numeric value but it also happens when your next sentence is not started with a capital letter e.g.Hello everyone. two minutes to go.In fact these are the mistakes in your text writing and not any problem with AVSpeechSynthesizer. Grammatically if you want to write this text then it should be in one of the following ways.

    Hello everyone. Two minutes to go.

    or

    Hello everyone, 2 minutes to go.

    both of the above statements will give the exact answer for your question. another line can work for you but that is not an appropriate sentence. Hello everyone,. 2 minutes to go.

    Hope this can help