Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

AVSpeechSynthesizer with Swift

Hello All,

I want to use the AVSpeechSynthesizer to make an iOS iPhone App say "Hello World".

I can only find tutorials for Objective-C and I don't understand the Apple Documentary.

I understand that I need to pass the text into a AVSpeechUtterance and then use AVSpeechSynthesizer to make the App speech, but I have no clue how to do this in code.

I did the following in my ViewController for now:

import AVFoundations

let speechSynthesis: AVSpeechUtterance = AVSpeechUtterance(string: "Hello World")

this should create a Object of the AVSpeechUtterance class with the initial text value "Hello World" right?

From here I tried different ways to use a function that uses AVSpeechSynthesizer but I am always getting errors.

Can you help me please? Thanks, Tobias

3 Answers

Hello. Initializing the Utterance was no bad afford, but you have to do a little bit more to get your speech running. You had forgotten the most important thing.

First you must initialize a Speech Synthesizer. Then you have to pass the Utterance to the Synthesizer. I would recommend to configure the Utterance. As i found at stackOverflow the iOS8 System has a bug which causes the First Utterance to very silent. The which i had found is to place a singleCharacter Utterance at first and then let speak the real Utterance.

Heres is the Code for it:

        var speechsynt: AVSpeechSynthesizer = AVSpeechSynthesizer() //initialize the synthesizer

        //workaround for iOS8 Bug

        var beforeSpeechString : String = " "
        var beforeSpeech:AVSpeechUtterance = AVSpeechUtterance(string: beforeSpeechString)
        speechsynt.speakUtterance(beforeSpeech)
        //realstring to speak
        var speechString : String = "Hello World! This is a Speech from iphone"

        var nextSpeech:AVSpeechUtterance = AVSpeechUtterance(string: speechStringGermanRepresentation)
        nextSpeech.rate = AVSpeechUtteranceMinimumSpeechRate; // some Configs :-)
        speechsynt.speakUtterance(nextSpeech) //let me Speak!
       //Debugging  
        println("Hello Swift World")
        println("\(beforeSpeech)")
        println("\(nextSpeech)")

Hope i helped you..:-) Feedback is very welcomed

Greets Thorsten

Hello Thorsten thank you for the reply.

I think I now got how to use the Utterance and Synthesizer, but I now run into some errors :)

When I use your code I get troubles with the GermanRepresentation added to speechString.

When I remove that I got the following error for each Utterance I pass to the Synthesizer: Speech initialisation error: 2147483665

After searching a bit in the web I found out that it might be that I need to activate SpeechSelection under Accessibility.

When I do that for my Simulator I get the following error after the error above: Cannot find executable for CFBundle 0x78e420f0 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/AccessibilityBundles/QuickSpeak.bundle (not loaded)

Is it just not possible to do this on a Simulator?

Thanks,

Tobias

Hello Tobias,

sorry the germanrepresentation was my fault. It is a var which i had used to test the speech in german because i am from germany. instead of that, use the speechString variable. then it should work. For being sure i had tested it on my device. Greets Thorsten

Cool Danke Dir :)

I wanted to learn a bit before buying an account, but not the time might have come :D