The reference that you need in order to use the text to speech classes:
@import AVFoundation;
Sample code:
AVSpeechSynthesizer* speaker = [[AVSpeechSynthesizer alloc] init];
AVSpeechUtterance* u = [[AVSpeechUtterance alloc] initWithString:@"Good morning. Boss."];
u.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
[speaker speakUtterance:u];
To get the list of the supported languages.
NSArray<AVSpeechSynthesisVoice *> * v = [AVSpeechSynthesisVoice speechVoices];
AVSpeechSynthesisVoice* v2;
for (int i = 0; i < v.count; i++) {
v2 = [v objectAtIndex:i];
NSLog(@"voice lang id=%@, code=%@", v2.identifier, v2.language);
}
Please take note that there is duplicate entry for the same language code. This is because it has male and female voices and the identifier to the voice is different (i.e., unique). You need to call voiceWithIentifier method instead of voiceWithLanguage.
No comments:
Post a Comment