Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSSpeechSynthesizer Change the language

I'm writing a simple program based in an example of a Cocoa Book that uses a NSSpeechSynthesizer to speak phrases. I would like to know how can I change the language used to synthesize the phase.

#import "PHAppDelegate.h"

@implementation PHAppDelegate

@synthesize window = _window;
@synthesize textField = _textField;

- (id)init{

    self = [super init];

    if(self){

        NSLog(@"init");

        _speechSynth = [[NSSpeechSynthesizer alloc] initWithVoice:nil];

    }

        return self;

}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
}

- (IBAction)stopIt:(id)sender {

    NSLog(@"stoppping");
    [_speechSynth stopSpeaking];

}

- (IBAction)sayit:(id)sender {

    NSString *string = [_textField stringValue];

    if([string length] == 0){

        NSLog(@"There is no text to speech.");

        return;
    }

    NSString *voiceID =[[NSSpeechSynthesizer availableVoices] objectAtIndex:10];

    [_speechSynth setVoice:voiceID];

    [_speechSynth startSpeakingString:string];


    NSLog(@"Have started to say: %@", string);


}
@end

This code works fine.

like image 401
user1821998 Avatar asked Dec 01 '25 12:12

user1821998


1 Answers

for (NSString *voiceIdentifierString in [NSSpeechSynthesizer availableVoices]) {
    NSString *voiceLocaleIdentifier = [[NSSpeechSynthesizer attributesForVoice:voiceIdentifierString] objectForKey:NSVoiceLocaleIdentifier];
    NSLog(@"%@ speaks %@", voiceIdentifierString, voiceLocaleIdentifier);

}

See Voice Attributes Keys

like image 129
geowar Avatar answered Dec 03 '25 04:12

geowar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!