Reputation: 109
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..
Upvotes: 5
Views: 6865
Reputation: 2359
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;
}
Upvotes: 4
Reputation: 341
It indeed a bug in XCode 6 and iOS 8.0.2. As long as you configured the "voiceWithLanguage" property, it won't speak the sentence for the first time. So the current solution is to speak an empty string initially with the desired language.
Upvotes: 1
Reputation: 4526
EDIT: the comment about recreating the synthesizer below appears to be relevant to iOS8.0.0 and GM only - in iOS8.0.2 it does not seem to be necessary and user848555's workaround works fine for me now. Interestingly user848555's workaround DOES seem to still be necessary (at least if you change languages), despite comments I have seen on other threads that it has been fixed in iOS8.0.2.
So this seems to be a bug related to low quality voices - it is referenced in the release notes here (search for Speech Synthesis on this page):
[https://developer.apple.com/library/ios/releasenotes/General/RN-iOSSDK-8.0/][1]
The offical Apple workaround is to download a high quality voice for the voice that is not working (by default one voice is always high quality - US English in English speaking nations, presumably this changes in other nations) which is why it seems to work if you stick to the default voice.
user848555's workaround does not work in isolation if (like me) you are constantly changing voices. However if you recreate the speechSynthesizer as well on each uttereance that appears to work. Here is my code (it is specific to my app so you will need to change a few things but it should illustrate the point). It uses user848555's fix of a blank utterence plus a recreate on each utterence.
-(void)speakGerman:(NSString*)text{
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1 ||
[self.dataController.speakSetting integerValue] == 2) return;
// iOS8 fix
speechSynthesizer = [[AVSpeechSynthesizer alloc]init];
AVSpeechUtterance *bugWorkaroundUtterance = [AVSpeechUtterance speechUtteranceWithString:@" "];
bugWorkaroundUtterance.rate = AVSpeechUtteranceMaximumSpeechRate;
[speechSynthesizer speakUtterance:bugWorkaroundUtterance];
AVSpeechUtterance *utterance;
utterance = [AVSpeechUtterance speechUtteranceWithString:text];
utterance.voice = germanVoice;
utterance.rate = 0.2;
utterance.preUtteranceDelay = 0.0;
[speechSynthesizer speakUtterance:utterance];
}
Ali [1]: https://developer.apple.com/library/ios/releasenotes/General/RN-iOSSDK-8.0/
Upvotes: 1
Reputation: 7778
I found this class solved the problem (or at least is a good workaround) for me...
https://github.com/dale-moore/iOSTextToSpeech
Upvotes: 0
Reputation: 181
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];
Upvotes: 9