2014-01-21 57 views
0

我是Objective-C的新手。目前我试图用NSTask执行跛脚。下面的代码似乎工作,因为Xcode's输出空间显示我lame的标准输出,即显示与终端上的lame输出相同。执行NSTask,但没有输出跛脚

但我不能在我的桌面上得到任何输出文件,即test.mp3。为什么我不能得到任何输出?我的代码有问题吗?

NSTask *task = [[NSTask alloc] init]; 
[task setLaunchPath:@"/usr/local/bin/lame"]; 
[task setArguments:[NSArray arrayWithObjects:@"/Users/xanadu62/Music/test.wav",nil]]; 
[task setStandardOutput:[NSFileHandle fileHandleForWritingAtPath:@"/Users/xanadu62/Desktop/test.mp3"]]; 
[task launch]; 

此外,我想使用“--preset extreme”作为跛脚选项。但是“任务setArguments:”不允许使用此选项作为参数。我想知道我该如何解决这个问题。

+0

你不及格壳路径NSTask。其实,对不起...我猜这样会没事的,因为没有名字包含空格。同时,不跛取一些设置来配置?你没有NSPipe。 –

回答

1

试试这样说:

NSTask *task = [[NSTask alloc] init]; 

[task setLaunchPath:@"/usr/local/bin/lame"]; 

[task setArguments: [NSArray arrayWithObjects: 
            @"--preset", 
            @"extreme", 
            @"/Users/xanadu62/Music/test.wav", 
            @"/Users/xanadu62/Desktop/test.mp3", 
            nil] 
]; 

[task launch]; 

你并不需要使用管道。

usage: lame [options] <infile> [outfile] 

    <infile> and/or <outfile> can be "-", which means stdin/stdout. 
+0

谢谢。你的代码工作正常。 – xanadu6291

+0

不错,反馈的txs! – 2014-01-21 17:04:57

+0

顺便说一句:http://boredzo.org/make-ram-disk/显示NSTask公司在行动(源代码在页面底部)。 – 2014-01-21 22:30:02

1

没用过跛,但通过查看文档的正确终端命令将是

“跛脚--preset极端/Users/lawrencepires/Desktop/test.mp3 /用户/ lawrencepires /桌面/ test1.mp3“

test.mp3是输入文件,test1.mp3是输出文件。

工作守则 - (可能是值得改变活输出)

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 
// Insert code here to initialize your application 


[self lameconvert]; 
} 


- (void)lameconvert { 

NSTask *task = [[NSTask alloc] init]; 
[task setLaunchPath:@"/usr/local/bin/lame"]; 
NSArray *argArray = [NSArray arrayWithObjects:@"--preset",@"extreme",@"/Users/xanadu62/Music/test.wav",@"/Users/xanadu62/Music/test.wav",nil]; 

[task setArguments:argArray]; 

[task launch]; 
[task waitUntilExit]; 
NSLog(@"Conversion Complete"); 


} 
@end 
+0

谢谢你的回答也解决了我的问题。但接受的答案是一个。所以我把第一个答案标记为接受的答案。 – xanadu6291