Playing With the Simple "AVSpeechSynthesizer" API's


iOS7 provides a way to use speech (Kind of text to speech conversion).

To create an object, you do as below :
AVSpeechSynthesizer *speechSynthesizer = [[AVSpeechSynthesizer alloc] init];
normally like any other NSObject.

To let it speak, we need to pass the “AVSpeechUtterance” object to it using this API:
[speechSynthesizer speakUtterance:/* AVSpeechUtterance object */];


So, to create an object for “AVSpeechUtterance”, you do as below :
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@“Hey baby!”];
So, Utterance is the one that provides the text which will be spoken by the synthesizer.

Also, it has few interesting properties, which could be tweaked based on our need.
Like rate, volume, pitchMultiplier, preUtteranceDelay, postUtteranceDelay

For example to slow down the speech, we could use :
utterance.rate *= 0.5;

whereas, preUtteranceDelay, postUtteranceDelay are used to delay the speech before and after starting the next in the queue respectively.

We also need to provide the voice to the utterance object, dictating it to speak in the specified language.

So, there are several voices available.

You can get them through this API :
[AVSpeechSynthesisVoice speechVoices]

It just returns an array and displays a list of the following :

2013-10-25 00:33:54.637 Voices[4509:70b] (
    "[AVSpeechSynthesisVoice 0x97681c0] Language: th-TH",
    "[AVSpeechSynthesisVoice 0x9727aa0] Language: pt-BR",
    "[AVSpeechSynthesisVoice 0x972c180] Language: sk-SK",
    "[AVSpeechSynthesisVoice 0x9764b60] Language: fr-CA",
    "[AVSpeechSynthesisVoice 0x9764bb0] Language: ro-RO",
    "[AVSpeechSynthesisVoice 0x9720a80] Language: no-NO",
    "[AVSpeechSynthesisVoice 0x9768520] Language: fi-FI",
    "[AVSpeechSynthesisVoice 0x97685d0] Language: pl-PL",
    "[AVSpeechSynthesisVoice 0x9768600] Language: de-DE",
    "[AVSpeechSynthesisVoice 0x9720a50] Language: nl-NL",
    "[AVSpeechSynthesisVoice 0x97682f0] Language: id-ID",
    "[AVSpeechSynthesisVoice 0x9768340] Language: tr-TR",
    "[AVSpeechSynthesisVoice 0x9768390] Language: it-IT",
    "[AVSpeechSynthesisVoice 0x97683e0] Language: pt-PT",
    "[AVSpeechSynthesisVoice 0x9768410] Language: fr-FR",
    "[AVSpeechSynthesisVoice 0x9764d40] Language: ru-RU",
    "[AVSpeechSynthesisVoice 0x9764d90] Language: es-MX",
    "[AVSpeechSynthesisVoice 0x9764e50] Language: zh-HK",
    "[AVSpeechSynthesisVoice 0x9764ea0] Language: sv-SE",
    "[AVSpeechSynthesisVoice 0x97682d0] Language: hu-HU",
    "[AVSpeechSynthesisVoice 0x9764fc0] Language: zh-TW",
    "[AVSpeechSynthesisVoice 0x9765010] Language: es-ES",
    "[AVSpeechSynthesisVoice 0x9765060] Language: zh-CN",
    "[AVSpeechSynthesisVoice 0x97650b0] Language: nl-BE",
    "[AVSpeechSynthesisVoice 0x9765100] Language: en-GB",
    "[AVSpeechSynthesisVoice 0x9765150] Language: ar-SA",
    "[AVSpeechSynthesisVoice 0x97651a0] Language: ko-KR",
    "[AVSpeechSynthesisVoice 0x97651f0] Language: cs-CZ",
    "[AVSpeechSynthesisVoice 0x9765240] Language: en-ZA",
    "[AVSpeechSynthesisVoice 0x9768550] Language: en-AU",
    "[AVSpeechSynthesisVoice 0x97685a0] Language: da-DK",
    "[AVSpeechSynthesisVoice 0x9765390] Language: en-US",
    "[AVSpeechSynthesisVoice 0x97653e0] Language: en-IE",
    "[AVSpeechSynthesisVoice 0x9765430] Language: hi-IN",
    "[AVSpeechSynthesisVoice 0x9765480] Language: el-GR",
    "[AVSpeechSynthesisVoice 0x97654d0] Language: ja-JP"
)


You can use any of these voice variants.

To create an object for voice, you do as below :
AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
If it returns nil, then the language might be wrong and not recognised.

So, now, we pass this voice to the utterance and utterance in turn to the synthesiser like below :

utterence.voice = voice;
[speechSynthesizer speakUtterance:utterance];

you can pass any number of utterances, and it’ll just queue up. 
If you pass the same object again to the queue, then it’ll thrown an exception on your face, Beware :) .


Comments

Popular posts from this blog

Free Hand Drawing on Google's Map View

Android from iOS Guy's Perspective

Free Hand Drawing on Maps with MapBox SDK