简体   繁体   English

AVAudioRecorder不会在设备上录制

[英]AVAudioRecorder Won't Record On Device

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. playBool是一个BOOL,当它处于播放模式时为YES。 Everything works in the simulator however, when I run it on a device, [recorder record] returns NO. 一切都在模拟器中工作,但是当我在设备上运行时,[记录器记录]返回NO。 Does anyone have a clue as to why this is happening? 有没有人知道为什么会这样?

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. 设置错误的AVAudioSession类别将导致记录/播放失败。

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. 您可以录制到应用程序的docs文件夹或tmp目录。

Ok, solved my own question; 好的,解决了我自己的问题; I don't know why, but I have to record to the NSTemporaryDirectory when on a device. 我不知道为什么,但我必须在设备上录制到NSTemporaryDirectory。 Changing that completely fixed it. 改变它完全修复它。

此外,请确保您的应用程序可以访问麦克风(即在iOS设置中,向下滚动并找到您的应用程序,它应该有'麦克风',您需要切换到'打开')

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. AVAudioRecorder初始化很好 - 没有事故 - 但iOS根本不允许我写它。 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. 谢谢你指导我正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM