AVSpeechSynthesizer与ios8

你有没有人在iOS 8上试过AvSpeechSynthesizer? 我在Xcode 6上做了快速的应用程序,但是没有audio出现,我在Xcode 5上运行了相同的程序,并且顺利地运行。

来自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]; 

错误与Xcode6?

==编辑===看起来像错误/问题与iOS 8 SIM卡,iOS7.1 SIM卡与Xcode6工作正常..

是的,它是一个错误。 截至iOS8 GM,似乎第一次尝试让AVSpeechSynthesizer说AVSpeech的话会导致沉默。

我的解决方法是使AVSpeechSynthesizer在初始化后立即说出单个字符的话语。 这将是沉默,然后你的AVSpeechSynthesizer将正常工作。

 AVSpeechUtterance *bugWorkaroundUtterance = [AVSpeechUtterance speechUtteranceWithString:@" "]; bugWorkaroundUtterance.rate = AVSpeechUtteranceMaximumSpeechRate; [self.speechSynthesizer speakUtterance:bugWorkaroundUtterance]; 

这在8.0.2上仍然是个问题(Xcode 6.0.1,在iPad mini 1上testing)。

这只是一个问题,如果您将AVSpeechUtterance的语音属性设置为默认方言以外的语言 (在我的情况下,英语(美国)是默认的)。 要明确,问题可以通过强制AVSpeechSynthesizer以非默认语言讲一个空的AVSpeechUtterance实例来解决。 最后一部分是重要的。

下面是一个简单的getter实现一个修复。

 - (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; } 

编辑:关于重新创build下面的合成器的评论似乎与iOS8.0.0和GM只有相关 – 在iOS8.0.2似乎并不需要和现在的用户848555的解决方法正常工作。 有趣的是,user848555的解决方法似乎仍然是必要的(至less如果你改变语言),尽pipe我已经看到在其他线程已经在iOS8.0.2中已经修复的意见。

因此,这似乎是一个与低质量声音相关的错误 – 它在发行说明中引用(在此页面上search语音合成):

[ https://developer.apple.com/library/ios/releasenotes/General/RN-iOSSDK-8.0/%5D [1 ]

苹果官方的解决方法是下载一个高质量的声音,对于不工作的声音(默认情况下,一个声音总是高质量的 – 在英语国家美国英语,大概这在其他国家的变化),这就是为什么它似乎工作如果你坚持默认的声音。

如果(像我一样)你不断地改变声音,user848555的解决方法不能单独工作。 但是,如果您重新创buildspeechSynthesizer以及似乎工作的每个发言。 这里是我的代码(这是特定于我的应用程序,所以你需要改变一些东西,但它应该说明这一点)。 它使用user848555的空白语言的修复加上每个说法重新创build。

 -(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]; } 

这确实是XCode 6和iOS 8.0.2中的一个错误。 只要你configuration了“voiceWithLanguage”属性,它就不会第一次说出这个句子。 所以目前的解决办法是先用所需的语言说出一个空string。

我发现这个类解决了问题(或者至less是一个很好的解决方法)对我来说…

https://github.com/dale-moore/iOSTextToSpeech