AVAudioRecorder Won't Record On Device

Posted by Dyldo42 on Stack Overflow See other posts from Stack Overflow or by Dyldo42
Published on 2010-04-20T15:16:43Z Indexed on 2010/04/22 2:13 UTC
Read the original article Hit count: 465

This is my method:

-(void) playOrRecord:(UIButton *)sender {
    if (playBool == YES) {
        NSError *error = nil;
        NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat: @"%d", [sender tag]] ofType:@"caf"];
        NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
        AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:&error];
        [player setNumberOfLoops:0];
        [player play];      
    }
    else if (playBool == NO) {
        if ([recorder isRecording]) {
            [recorder stop];
            [nowRecording setImage:[UIImage imageNamed:@"NormalNormal.png"] forState:UIControlStateNormal];
            [nowRecording setImage:[UIImage imageNamed:@"NormalSelected.png"] forState:UIControlStateSelected];
        }
        if (nowRecording == sender) {
        nowRecording = nil;
        return;
        }
        nowRecording = sender;
        NSError *error = nil;
        NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat: @"%d", [sender tag]] ofType:@"caf"];
        NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
        [sender setImage:[UIImage imageNamed:@"RecordingNormal.png"] forState:UIControlStateNormal];
        [sender setImage:[UIImage imageNamed:@"RecordingSelected.png"] forState:UIControlStateSelected];        
        recorder = [[AVAudioRecorder alloc] initWithURL:fileUrl settings:recordSettings error:&error];
        [recorder record];
    }
}

Most of it is self explanatory; playBool is a BOOL that is YES when it is in play mode. Everything works in the simulator however, when I run it on a device, [recorder record] returns NO. Does anyone have a clue as to why this is happening?

© Stack Overflow or respective owner

Related posts about iphone-sdk

Related posts about avaudiorecorder