AVAudioRecorder Won't Record On Device

后端 未结 5 1623
失恋的感觉
失恋的感觉 2021-01-02 22:28

This is my method:

-(void) playOrRecord:(UIButton *)sender {
    if (playBool == YES) {
        NSError *error = nil;
        NSString *filePath = [[NSBundle         


        
相关标签:
5条回答
  • 2021-01-02 22:56

    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.

    0 讨论(0)
  • 2021-01-02 23:00

    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.

    0 讨论(0)
  • 2021-01-02 23:02

    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')

    0 讨论(0)
  • 2021-01-02 23:03

    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.

    0 讨论(0)
  • 2021-01-02 23:07

    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.

    0 讨论(0)
提交回复
热议问题