NSSpeechSynthesizer更改语言

时间:2012-11-13 20:47:21

标签: objective-c xcode cocoa

我正在编写一个基于Cocoa Book示例的简单程序,该程序使用NSSpeechSynthesizer来说短语。我想知道如何更改用于合成阶段的语言。

#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

此代码工作正常。

2 个答案:

答案 0 :(得分:1)

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

}

请参阅Voice Attributes Keys

答案 1 :(得分:0)

this 2014 document说只有一种语言可用(美国英语)。

NSVoiceLanguage
The language of the voice (currently US English only). An NSString Deprecated: Use NSVoiceLocaleIdentifier (page 24) instead. Available in OS X v10.0 and later.
Deprecated in OS X v10.5.
Declared in NSSpeechSynthesizer.h.