Reputation: 19
My code speaks a text in one language and switches to another language (if necessary). The Problem is, that only the first Sentence is spoken. I need a possibility to speak more sentences in more than one language. I need different languages, and this needs a new initialization. If the first sentence is very short, it sometimes speaks the second one as well. The third is never spoken. An Suggestions?
- (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
}
}
Upvotes: 1
Views: 532
Reputation: 205
Same things happens to me. I guess it is a bug on iOS 8 SDK.
I actually found workaround by setting preUtteranceDelay
to 0.05 (or more) property of AVSpeechUtterance
.
Upvotes: 1