Mick F
Mick F

Reputation: 7439

How to programmatically use iOS voice synthesizers? (text to speech)

iOS devices have embedded voice synthesizers for Accessibility's VoiceOver feature. Is there a way you can use these synthesizers programmatically to generate text-based sounds?

My problem is: I'm working on a simple app for kids to learn colors and rather than recording the names of the colors in each language i want to support and storing them as audio files, i'd rather generate the sounds at runtime with some text-to-speech feature.

Thanks

[EDIT: this question was asked pre-iOS7 so you should really consider the voted answer and ignore older ones, unless you're a software archeologist]

Upvotes: 42

Views: 37026

Answers (6)

Onato
Onato

Reputation: 10221

Starting from iOS 7, Apple provides this API.

Objective-C

#import <AVFoundation/AVFoundation.h>
…
AVSpeechUtterance *utterance = [AVSpeechUtterance 
                            speechUtteranceWithString:@"Hello World!"];
AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc] init];
[synth speakUtterance:utterance];

Swift

import AVFoundation
…
let utterance = AVSpeechUtterance(string: "Hello World!")
let synth = AVSpeechSynthesizer()
synth.speakUtterance(utterance)

Upvotes: 71

Alex Malko
Alex Malko

Reputation: 126

This code worked for me with Swift and iOS 8 on both Simulator and iPhone 6. I needed to add the standard AVFoundation library:

import AVFoundation

// ...

func onSayMeSomething() {
    let utterance = AVSpeechUtterance(string: "Wow! I can speak!")
    utterance.pitchMultiplier = 1.3
    utterance.rate = AVSpeechUtteranceMinimumSpeechRate * 1.5
    let synth = AVSpeechSynthesizer()
    synth.speakUtterance(utterance)
}

Upvotes: 6

user2518512
user2518512

Reputation: 240

#import <AVFoundation/AVFoundation.h>

AVSpeechSynthesizer *av = [[AVSpeechSynthesizer alloc] init];
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc]initWithString:@"Text to say"]; 
[av speakUtterance:utterance];

Upvotes: 11

Demz
Demz

Reputation: 1058

You can try these third party APIs: iSpeech or OpenEars

Upvotes: 0

Duke
Duke

Reputation: 1731

you could find this helpful Making Your iPhone Application Accessible

As stated in “iPhone Accessibility API and Tools,” standard UIKit controls and views are automatically accessible. If you use only standard UIKit controls, you probably don’t have to do much additional work to make sure your application is accessible. In this case, your next step is to ensure that the default attribute information supplied by these controls makes sense in your application. To learn how to do this, see “Supply Accurate and Helpful Attribute Information.”

Upvotes: 0

yuji
yuji

Reputation: 16725

Unfortunately iOS doesn't expose a public API for programmatically generating speech.

There is a private API you can use, if you're not submitting to the App Store.

Otherwise, see the responses to this question for a number of third-party libraries you can use.

Upvotes: 3

Related Questions