Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVSpeechSynthesizer with ios8

HI did anyone tried AvSpeechSynthesizer on iOS 8 ? I did quick app on Xcode 6 but no audio comes out, I ran the same on on the Xcode 5 and works without a hitch.

sample code from http://nshipster.com/avspeechsynthesizer/

NSString *string = @"Hello, World!";
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:string];
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];

AVSpeechSynthesizer *speechSynthesizer = [[AVSpeechSynthesizer alloc] init];
[speechSynthesizer speakUtterance:utterance];

Bug with Xcode6?

== Edit === Looks like bug/issue with iOS 8 sim, iOS7.1 Sim with Xcode6 works fine..

like image 502
Greyeye Avatar asked Jun 13 '14 04:06

Greyeye


2 Answers

Yup, its a bug. As of iOS8 GM, it seems the first attempt to get AVSpeechSynthesizer to speak an AVSpeechUtterance will result in silence.

My workaround is to make AVSpeechSynthesizer speak a single-character utterance immediately after it is initialized. This will be silent, and afterwards your AVSpeechSynthesizer will work as normal.

AVSpeechUtterance *bugWorkaroundUtterance = [AVSpeechUtterance speechUtteranceWithString:@" "];
bugWorkaroundUtterance.rate = AVSpeechUtteranceMaximumSpeechRate;
[self.speechSynthesizer speakUtterance:bugWorkaroundUtterance];
like image 98
user848555 Avatar answered Sep 30 '22 19:09

user848555


This is still an issue on 8.0.2 (Xcode 6.0.1, testing on iPad mini 1).

This is only an issue if you are setting the voice property for AVSpeechUtterance to a language other than the default dialect (in my case English (U.S.) is default). To be clear, the issue can be resolved by forcing AVSpeechSynthesizer to speak an empty AVSpeechUtterance instance in the non default language first. That last part is important.

Below is a simple getter that implements a fix.

- (AVSpeechUtterance *)utterance {
    if (!_utterance) {
        ...
        _utterance = [AVSpeechUtterance speechUtteranceWithString:@"hello world"];
        [_utterance setRate:0.2f];
        [_utterance setVoice:[AVSpeechSynthesisVoice voiceWithLanguage:language]];

        // iOS 8 bug fix
        AVSpeechUtterance *dummyUtterance = [AVSpeechUtterance speechUtteranceWithString:@" "];
        [dummyUtterance setVoice:[AVSpeechSynthesisVoice voiceWithLanguage:language]];
        [self.synthesizer speakUtterance:dummyUtterance];
    }
    return _utterance;
}
like image 41
Matt Avatar answered Sep 30 '22 18:09

Matt