Question

I have created one test application for iPhone. I wrote the following code to record. But it is not working.


-(IBAction)start:(id)sender{
    recordSetting = [[NSMutableDictionary alloc] init];
    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
    [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
    [recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
    [recordSetting setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
    [recordSetting setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    soundsDirectoryPath = [documentsDirectory stringByAppendingPathComponent:@"Sounds"];
    [[NSFileManager defaultManager] createFileAtPath:[NSString stringWithFormat:@"%@/Test.caf", soundsDirectoryPath] contents:nil attributes:nil];

    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Test.caf", soundsDirectoryPath]];
    NSError *err = nil;
    if([recorder isRecording])
    {
        [recorder stop];
        recorder = nil;
    }
    recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];
    [recorder setDelegate:self];
    recorder.meteringEnabled = YES;
    if(err){
        NSLog(@"Error : %@", [err description]);
    }
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    BOOL audioHWAvailable = audioSession.inputIsAvailable;
    if (! audioHWAvailable) {
        UIAlertView *cantRecordAlert =
        [[UIAlertView alloc] initWithTitle: @"Warning"
                                   message: @"Audio input hardware not available"
                                  delegate: nil
                         cancelButtonTitle:@"OK"
                         otherButtonTitles:nil];
        [cantRecordAlert show];
        [cantRecordAlert release]; 
        return;
    }
    BOOL st = [recorder prepareToRecord];
    if(!st){
        NSLog(@"Failed");
    }
    recorder.meteringEnabled = YES;
    BOOL status = [recorder record];
    if(!status)
    NSLog(@"Failed");
}

-(IBAction)stop:(id)sender{
    if([recorder isRecording]){
        NSLog(@"Recording...");
    }else{
        NSLog(@"Not recording...");
    }
    [recorder stop];
}

When I call "start" method, it is printing "Failed" after calling "prepareToRecord", and "record" methods.

When I call "stop" method, it is printing "Not recording..." message.

what mistake I have made in this code.

Thanks.

Was it helpful?

Solution

Based on the extra information you provided in the question comments, it appears that there is a problem when creating the file associated with the recorder. This is indicated by the NO value returned by the prepareToRecord method.

One thing to note is that prepareToRecord creates the recording file on its own, using the URL supplied in the init method of AVAudioRecorder. There is no need to explicitly create it, as you do using NSFileManager.

Try logging the soundsDirectoryPath, and ensure that it's a valid file path.

EDIT: Found the real problem. You need to create the directory before the file is created. Replace this line:

[[NSFileManager defaultManager] createFileAtPath:[NSString stringWithFormat:@"%@/Test.caf", soundsDirectoryPath] contents:nil attributes:nil];

With this one:

[[NSFileManager defaultManager] createDirectoryAtPath:soundsDirectoryPath withIntermediateDirectories:YES attributes:nil error:NULL];

The new line only creates the directory and not the file, but that's fine because the prepareToRecord method creates the file automatically.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top