iOS Text to Speech API

iOS Text To Speech Api

Since iOS 7 you have a new TTS Api.

In Objective C

AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc]init];
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"Some text"];
[utterance setRate:0.2f];
[synthesizer speakUtterance:utterance];

In Swift

let synthesizer = AVSpeechSynthesizer()
let utterance = AVSpeechUtterance(string: "Some text")
utterance.rate = 0.2

You can also change the voice like this :

utterance.voice = AVSpeechSynthesisVoice(language: "fr-FR")

And then speek

  • In Swift 2
    synthesizer.speakUtterance(utterance)

  • In Swift 3
    synthesizer.speak(utterance)

Don't forget to import AVFoundation

Helpful methods

You can Stop or Pause all speech using these two methods :

- (BOOL)pauseSpeakingAtBoundary:(AVSpeechBoundary)boundary;
- (BOOL)stopSpeakingAtBoundary:(AVSpeechBoundary)boundary;

The AVSpeechBoundary indicates if the speech should pause or stop immediately (AVSpeechBoundaryImmediate) or it should pause or stop after the word currently being spoken (AVSpeechBoundaryWord).

Check the AVSpeechSynthesizer Doc

iPhone - API for Text to Speech feature

I ran into this problem at one point, and got Flite (festival lite) text to speech engine running on iPhone. I recently made an API class for it. I tried to make it easy (and free) to add to new and existing projects. It can be downloaded at here.

How do we add text-to-speech functionality in iOS 12?

I think you should try google Cloud speech Api :

https://cloud.google.com/speech-to-text/

https://github.com/GoogleCloudPlatform/ios-docs-samples/tree/master/speech/Objective-C

tutorial : https://medium.com/google-cloud/how-to-integrate-google-cloud-text-to-speech-api-into-your-ios-app-140ab7be42ae

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

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)


Related Topics



Leave a reply



Submit