0

I'm new to Objective-C. Currently I'm trying to execute lame with NSTask. The following code seems to be working because Xcode's output space shows me lame's standardoutput i.e. shows same as lame's output on Terminal.

But I can't get any output file i.e. test.mp3 on my desktop. Why I can't get any output? Is there any wrong with my code?

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];

Also, I'd like to use "--preset extreme" as lame option. But "task setArguments:" doesn't allow to use this option as argument. I'd like to know how can I solve this issue too.

Sport
  • 8,570
  • 6
  • 46
  • 65
xanadu6291
  • 931
  • 10
  • 20
  • You are not passing shell paths to NSTask. Actually, sorry... I guess that'll be okay since no name contains space. Meanwhile, doesn't lame take some settings to configure? And you have no NSPipe. – El Tomato Jan 21 '14 at 15:51

2 Answers2

1

Try it this way:

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];

You don't need to use pipes.

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

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

Never used lame, but by looking at the docs the correct terminal command would be

"lame --preset extreme /Users/lawrencepires/Desktop/test.mp3 /Users/lawrencepires/Desktop/test1.mp3"

test.mp3 being the input file, test1.mp3 being the output file.

Working Code - (may be worth changing for live output)

- (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
LawrencePires
  • 88
  • 1
  • 5
  • Thanks your answer also solve my question. But accepted answer is to be one. So I marked first answer as accepted answer. – xanadu6291 Jan 21 '14 at 16:51