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..
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];
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With