objective-c - NSSpeechSynthesizer 更改语言

标签 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

这段代码工作正常。

最佳答案

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

参见 Voice Attributes Keys

关于objective-c - NSSpeechSynthesizer 更改语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13368516/

相关文章:

XCode 5.0 不明确的布局警告

ios - NSURLSession 操作指南

ios - 在第三方应用程序之间共享数据

iphone - 测试模拟器和设备时自动切换 Xcode Config

ios - 在 Swift 4 中显示实时时间标签?

objective-c - NSNumberFormatter 干扰 NSString intValue

xcode - cocoa : How to intercept a right click on a free (no icon) area of the desktop?

iphone - Objective-C 子类问题

iphone - 如何从UIAlertView更改键盘颜色

xcode - 如何在 swift 中将闭包作为参数传递(只是基础知识)