AVAudioRecorder不会在设备上录制 [英] AVAudioRecorder Won't Record On Device

查看:113
本文介绍了AVAudioRecorder不会在设备上录制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的方法:

-(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];
    }
}

大多数是自我解释的; playBool是一个BOOL,当它处于播放模式时为YES。
一切都在模拟器中工作,但是当我在设备上运行时,[记录器记录]返回NO。有没有人知道为什么会发生这种情况?

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?

推荐答案

好的,解决了我自己的问题;我不知道为什么,但我必须在设备上录制到NSTemporaryDirectory。改变它完全修复它。

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.

这篇关于AVAudioRecorder不会在设备上录制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆