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

6

41

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]

Chitchat answered 30/3, 2012 at 8:38 Comment(0)
C
71

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)
Camphor answered 19/9, 2013 at 9:47 Comment(3)
Can we save the uttered voice as an mp3 file?Dora
@Camphor How can I stop in text to speech.?Fag
Hey is there any way to play small audio files with iOS-like in android there is a thing called earcons which lets you play audio files.Paulpaula
B
11
#import <AVFoundation/AVFoundation.h>

AVSpeechSynthesizer *av = [[AVSpeechSynthesizer alloc] init];
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc]initWithString:@"Text to say"]; 
[av speakUtterance:utterance];
Boudoir answered 28/6, 2013 at 7:41 Comment(1)
Can we save the uttered voice to mp3 file?Dora
G
6

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)
}
Girlfriend answered 16/9, 2015 at 1:51 Comment(0)
F
3

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.

Fanya answered 30/3, 2012 at 8:46 Comment(4)
Let's wish Apple people will come over here and read this as a "please-make-the-api-public" request ;)Chitchat
Apple does not pay any attention even to the requests directly posted to them so I would not hope too much :(Betty
as a matter of fact, Apple did it. :)Chitchat
Note that this answer was correct when it was posted, but as noted in other answers, Apple added a public API in iOS 7.Pentylenetetrazol
R
0

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.”

Renshaw answered 30/3, 2012 at 8:46 Comment(0)
G
0

You can try these third party APIs: iSpeech or OpenEars

Gimpel answered 30/3, 2012 at 8:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.