iOS Text To Speech Api

IosText to-SpeechSiri

Ios Problem Overview


I can't seem to find anything on this. Are there any Siri classes or API's in iOS7 that let you do text to speech? All I am trying to do is something like the following:

[siriInstance say:@"This is a test"];

And then have Siri say it from my app.

It seems we should be capable of doing this, no? Seems like a trivial thing.

Ios Solutions


Solution 1 - Ios

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

Solution 2 - Ios

This is Ali ABBAS' answer for use in a playground:

import UIKit
import AVKit
import AVFoundation
import PlaygroundSupport

var str = "Hello, playground"

let synthesizer = AVSpeechSynthesizer()
let utterance = AVSpeechUtterance(string: str)
utterance.rate = 0.4
utterance.voice = AVSpeechSynthesisVoice(language: "en-US")

//for playground only
let playerViewController = AVPlayerViewController()
PlaygroundPage.current.liveView = playerViewController.view
//
    
synthesizer.speak(utterance)    

Solution 3 - Ios

I have never done any work specifically with Siri. I may be wrong, but I think integrating with Siri is very difficult using private API's.

I would take a look at the openears framework for IOS. I have done some basic work with this in the past and it does both offline speech recognition and synthesized speech/text-to-speech

Hope this helps you.

Solution 4 - Ios

Here you will find a text to speech (TTS) sample app (Objective-C) based on this

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJesseView Question on Stackoverflow
Solution 1 - IosAli AbbasView Answer on Stackoverflow
Solution 2 - IosMatt BearsonView Answer on Stackoverflow
Solution 3 - IosTimView Answer on Stackoverflow
Solution 4 - Iosd1jhoni1bView Answer on Stackoverflow