Hello,
I would like to make a command line program that is able to record audio using the internal mike of my MBP. I've done some experimenting, but the problem is that as soon as a leave out the GUI the program won't run properly. It seems as if the program is 'waiting' for something. I've tried to run things in separate threats, which seems to help, as the code below works well.
However, as soon as I leave out starting NSApplicationMain, it stops working properly.
Any ideas on what I might have done wrong?
I would like to make a command line program that is able to record audio using the internal mike of my MBP. I've done some experimenting, but the problem is that as soon as a leave out the GUI the program won't run properly. It seems as if the program is 'waiting' for something. I've tried to run things in separate threats, which seems to help, as the code below works well.
However, as soon as I leave out starting NSApplicationMain, it stops working properly.
Any ideas on what I might have done wrong?
Code:
#import <Cocoa/Cocoa.h>
#import <QTKit/QTkit.h>
#import <pthread.h>
QTCaptureSession *mCaptureSession;
QTCaptureMovieFileOutput *mCaptureMovieFileOutput;
QTCaptureDeviceInput *mCaptureAudioDeviceInput;
pthread_t *t_cap, *t_stop;
void capture_tread(void *arch);
void capture_stop(void *arch);
int main(int argc, char *argv[])
{
pthread_create(&t_cap, NULL, capture_tread, NULL);
pthread_create(&t_stop, NULL, capture_stop, NULL);
printf("Starting threads...\n");
// As soon as I leave out this part (and add pthreads_join to ensure the main thread doesn't exit premature), no errors occur but sound will never be written to a file...
return NSApplicationMain(argc, (const char **) argv);
}
void capture_tread(void *arch)
{
printf("Start capture thread\n");
// Create the capture session
mCaptureSession = [[QTCaptureSession alloc] init];
// Connect inputs and outputs to the session
BOOL success = NO;
NSError *error;
QTCaptureDevice *audioDevice = [QTCaptureDevice defaultInputDeviceWithMediaType:QTMediaTypeSound];
success = [audioDevice open:&error];
if (!success) {
audioDevice = nil;
// Handle error
}
if (audioDevice) {
mCaptureAudioDeviceInput = [[QTCaptureDeviceInput alloc] initWithDevice:audioDevice];
success = [mCaptureSession addInput:mCaptureAudioDeviceInput error:&error];
if (!success) {
// Handle error
}
}
// Create the audi file output and add it to the session
mCaptureMovieFileOutput = [[QTCaptureMovieFileOutput alloc] init];
success = [mCaptureSession addOutput:mCaptureMovieFileOutput error:&error];
if (!success) {
// Handle error
}
//[mCaptureMovieFileOutput setDelegate:0];
// Set the compression for the audio/video that is recorded to the hard disk
NSEnumerator *connectionEnumerator = [[mCaptureMovieFileOutput connections] objectEnumerator];
QTCaptureConnection *connection;
// iterate over each output connection for the capture session and specify the desired compression
while ((connection = [connectionEnumerator nextObject])) {
NSString *mediaType = [connection mediaType];
QTCompressionOptions *compressionOptions = nil;
// specify the audio compression options
if ([mediaType isEqualToString:QTMediaTypeSound]) {
// use AAC Audio
compressionOptions = [QTCompressionOptions compressionOptionsWithIdentifier:@"QTCompressionOptionsHighQualityAACAudio"];
}
// set the compression options for the movie file output
[mCaptureMovieFileOutput setCompressionOptions:compressionOptions forConnection:connection];
}
[mCaptureSession startRunning];
[mCaptureMovieFileOutput recordToOutputFileURL:[NSURL fileURLWithPath:@"/Users/shared/test.mov"]];
}
void capture_stop(void *arch)
{
printf("Pausing...\n");
sleep(5);
[mCaptureMovieFileOutput recordToOutputFileURL:[NSURL fileURLWithPath:nil]];
printf("Stop capturing\n");
}
Last edited: