How to programmatically use iOS voice synthesizers? (text to speech)
Starting from iOS 7, Apple provides this API.
See this answer.
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)
#import <AVFoundation/AVFoundation.h>AVSpeechSynthesizer *av = [[AVSpeechSynthesizer alloc] init];AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc]initWithString:@"Text to say"]; [av speakUtterance:utterance];
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)}