5

嗨,有人在 iOS 8 上尝试过 AvSpeechSynthesizer 吗?我在 Xcode 6 上做了快速应用程序,但没有音频出来,我在 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的错误/问题,带有Xcode6的iOS7.1 Sim工作正常..

4

5 回答 5

9

是的,它是一个错误。从 iOS8 GM 开始,似乎第一次尝试让 AVSpeechSynthesizer 说出 AVSpeechUtterance 会导致静音。

我的解决方法是让 AVSpeechSynthesizer 在初始化后立即说出单字符话语。这将保持沉默,之后您的 AVSpeechSynthesizer 将正常工作。

AVSpeechUtterance *bugWorkaroundUtterance = [AVSpeechUtterance speechUtteranceWithString:@" "];
bugWorkaroundUtterance.rate = AVSpeechUtteranceMaximumSpeechRate;
[self.speechSynthesizer speakUtterance:bugWorkaroundUtterance];
于 2014-09-15T01:00:35.130 回答
4

这仍然是 8.0.2 的问题(Xcode 6.0.1,在 iPad mini 1 上测试)。

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

下面是一个实现修复的简单 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;
}
于 2014-10-23T14:41:17.160 回答
1

这确实是 XCode 6 和 iOS 8.0.2 中的一个错误。只要你配置了“voiceWithLanguage”属性,它就不会第一次说这句话。所以目前的解决方案是最初用所需的语言说一个空字符串。

于 2014-10-09T16:50:00.133 回答
1

编辑:下面关于重新创建合成器的评论似乎仅与 iOS8.0.0 和 GM 相关 - 在 iOS8.0.2 中似乎没有必要,user848555 的解决方法现在对我来说很好。有趣的是,user848555 的解决方法似乎仍然是必要的(至少如果您更改语言),尽管我在其他线程上看到它已在 iOS8.0.2 中修复的评论。

所以这似乎是与低质量语音相关的错误 - 它在此处的发行说明中被引用(在此页面上搜索语音合成):

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

Apple 的官方解决方法是为不工作的声音下载高质量的声音(默认情况下,一个声音始终是高质量的 - 英语国家的美国英语,可能在其他国家会发生变化),这就是为什么它似乎工作的原因你坚持默认的声音。

如果(像我一样)你不断地改变声音,user848555 的解决方法不能孤立地工作。但是,如果您在每个似乎有效的话语上重新创建 SpeechSynthesizer。这是我的代码(它特定于我的应用程序,因此您需要更改一些内容,但它应该说明这一点)。它使用 user848555 对空白话语的修复以及对每个话语的重新创建。

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

阿里[1]:https ://developer.apple.com/library/ios/releasenotes/General/RN-iOSSDK-8.0/

于 2014-09-27T12:46:57.577 回答
0

我发现这门课为我解决了这个问题(或者至少是一个很好的解决方法)......

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

于 2014-10-03T15:08:08.093 回答