AVAudioRecorder Won't Record On Device

此生再无相见时 提交于 2019-11-29 20:40:28

问题


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?


回答1:


If anywhere in your code you have this setup:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

Change it to:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

Setting the wrong AVAudioSession category will cause record/play to fail.




回答2:


This is because you cannot modify the app-bundle on the device ( it is signed ). You can record to either the docs folder for your app, or the tmp directory.




回答3:


Ok, solved my own question; I don't know why, but I have to record to the NSTemporaryDirectory when on a device. Changing that completely fixed it.




回答4:


I had the exact same problem. It turns out I was bumping up against an incorrectly generated path statement. The AVAudioRecorder initialized fine - and without incident - but iOS simply would not allow me to write to it. Why? Check it out:

    /* The following code will fail - why? It fails because it is trying to write some path that is NOT accessible by
    this app.
NSString *recordedAudioPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) 
                                objectAtIndex:0] 
                               stringByAppendingString:@"recorded.caf"];
 */
// Correction:
NSString *recordedAudioPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) 
                               objectAtIndex:0];

recordedAudioPath = [recordedAudioPath stringByAppendingPathComponent:@"recorded.caf"];
NSURL *recordURL = [NSURL fileURLWithPath:recordedAudioPath];

This makes complete sense now. Thanks for steering me in the right direction.




回答5:


Also, make sure your app is allowed to access the Microphone (ie in the iOS settings, scroll down and find your app, and it should have 'Microphone' which you need to switch to 'on')



来源:https://stackoverflow.com/questions/2676292/avaudiorecorder-wont-record-on-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!