本文介绍了NSSpeechSynthesizer更改语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

此代码可以正常工作.

推荐答案

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

}

请参见语音属性键

这篇关于NSSpeechSynthesizer更改语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 03:22