2012-08-25 41 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]]; 

然後很容易設置語音和調整其他設置。

相關問題