0
votes

I am struggling with the AVAudioRecorder (and Player) now. I have an application in which there are three simple buttons which do record/play/stop calls, and I'm using AVAudioRecorder and AVAudioPlayer to record and play the sound. Right now, when I test the app (both on the iPhone and in the simulator) I am getting the Cocoa 260 - file not found exception when trying to play what I just recorded. When printing the path to the AVAudioRecorder's file URL, I get an URL to a file inside a folder which does not exist on my computer (running on simulator. My code looks like this:

- (void)viewDidLoad
{
    [super viewDidLoad];

    _plyBtn.enabled = NO;
    _stopBtn.enabled = NO;

    NSArray *dirPaths;
    NSString *docsDir;

    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    docsDir = dirPaths[0];

    NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"test.caf"];

    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

    NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithInt:AVAudioQualityMin],
                                    AVEncoderAudioQualityKey,
                                    [NSNumber numberWithInt:16],
                                    AVEncoderBitRateKey,
                                    [NSNumber numberWithInt:2],
                                    AVNumberOfChannelsKey,
                                    [NSNumber numberWithFloat:44100.0],
                                    AVSampleRateKey,
                                    nil];

    NSError *error = nil;
    _rec = [[AVAudioRecorder alloc] initWithURL:soundFileURL settings:recordSettings error:&error];

    if(error){
        NSLog(@"error: %@", [error localizedDescription]);
    } else {
        [_rec prepareToRecord];
    }

    NSLog(@"path: %@", soundFileURL);
}

I have no clue why that NSLog at the bottom there prints an invalid file path... Like I said, I don't get the error when recording, but when attempting to play the audio which I recorded afterwards. Here's my play function:

- (IBAction)playAudio:(id)sender {
    if(!_rec.recording){
        _recBtn.enabled = NO;
        _stopBtn.enabled = YES;

        NSError *error1;
        NSError *error2;

        NSData *soundFile = [[NSData alloc] initWithContentsOfURL:_rec.url options:NSDataReadingMappedIfSafe error:&error1];
        _ply = [[AVAudioPlayer alloc] initWithData:soundFile error:&error2];

        //_ply = [[AVAudioPlayer alloc] initWithContentsOfURL:_rec.url error:&error];

        _ply.delegate = self;

        if(error1){
            NSLog(@"error: %@", [error1 localizedDescription]);
        } else if (error2) {
            NSLog(@"error: %@", [error2 localizedDescription]);
        } else {
            [_ply play];
        }
    }
}

This doesn't seem to be working:

- (IBAction)recordAudio:(id)sender {
    NSLog(@"recordAudio called");
    _plyBtn.enabled = NO;
    _stopBtn.enabled = YES;
    [_rec record];
}

Thanks for any help in advance :)

1

1 Answers

3
votes

Perhaps the recording path was invalid.

NSDocumentationDirectory... Shouldn't that be NSDocumentDirectory ?