使用AVSpeechSynthesizer更多句子的不同语言

时间:2014-10-14 20:58:01

标签: xcode avspeechsynthesizer

我的代码用一种语言说一个文本并切换到另一种语言(如果需要)。 问题是,只说了第一句。 我需要有可能用多种语言说更多的句子。我需要不同的语言,这需要一个新的初始化。 如果第一句话很短,它有时也会说第二句。第三个从未说过。 一个建议?

- (void) StartMyProgramm {
SpeakTextPrefix = @"This is the first Line";
SpeakTextTitle = @"De Titel van het boek is";
SpeakTextSuffix = @"Wie komme ich hier wieder raus";
if (![SpeakTextPrefix isEqual: @""]) {
    [self speakText:SpeakTextPrefix];

} else if (![SpeakTextTitle  isEqual: @""]) {
    [self speakText:SpeakTextTitle];

} else if (![SpeakTextSuffix  isEqual: @""]) {
    [self speakText:SpeakTextSuffix];

}
}
- (void) speakText:(NSString *)textToSpeak {
if (![textToSpeak  isEqual: @""]) {
    //Recognize Language
    NSArray *tagschemes = [NSArray arrayWithObjects:NSLinguisticTagSchemeLanguage, nil];
    NSLinguisticTagger *tagger = [[NSLinguisticTagger alloc] initWithTagSchemes:tagschemes options:0];
    [tagger setString:textToSpeak];
    NSString *language = [tagger tagAtIndex:0 scheme:NSLinguisticTagSchemeLanguage tokenRange:NULL sentenceRange:NULL];        
    //Speak
    AVSpeechSynthesisVoice *voice = nil;
    AVSpeechUtterance *utterance = nil;
    voice = [AVSpeechSynthesisVoice voiceWithLanguage:language];
    utterance = [[AVSpeechUtterance alloc] initWithString:textToSpeak];
    utterance.voice = voice;
    utterance.rate = 0.3;
    utterance.pitchMultiplier = 0.8;
    [self.synthesizer speakUtterance:utterance];
}
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance {
if (![SpeakTextTitle  isEqual: @""]) {
    [self speakText:SpeakTextTitle];
    SpeakTextTitle = @"";
} else if (![SpeakTextSuffix  isEqual: @""]) {
    [self speakText:SpeakTextSuffix];
    SpeakTextSuffix = @"";
} else {
    //do something else
}
}

1 个答案:

答案 0 :(得分:1)

同样的事情发生在我身上。我想这是iOS 8 SDK上的一个错误。

我实际上通过将preUtteranceDelay设置为AVSpeechUtterance的0.05(或更多)属性来找到解决方法。