2012-08-25 42 views
1

我试图制作一个Mac应用程序,用于说明用户输入的NSTextField文本,使用“system”在终端中使用say命令。但是,Xcode不断给出错误。在可可应用程序中通过终端讲话

- (IBAction)speak:(id)sender{ 
    system("say %@", [textinput stringValue]); 
} 

*的TextInput是NSTextFieldIBOutlet

+2

究竟是什么错误? –

+0

是否有任何理由试图让系统为此而不是NSSpeechSynthesizer类? – Abizern

+0

NSSpeechSynthesizer class?,之前听说过它,它是如何工作的? – haseo98

回答

4

系统采用单个字符*作为参数,所以你必须格式化命令字符串,然后才能将其传递给系统:

- (IBAction)speak:(id)sender { 
    NSString *command = [NSString stringWithFormat:@"say %@", [textinput stringValue]]; 
    system([command UTF8String]); 
} 
+0

感谢您的帮助:) – haseo98

3

有没有必要调出该系统命令,只是使用可可语音合成API直接。例如

NSSpeechSynthesizer* speechSynthesizer = [[NSSpeechSynthesizer alloc] init]; 
[speechSynthesizer startSpeakingString:[textinput stringValue]]; 

然后很容易设置语音和调整其他设置。

相关问题