【问题标题】:AVAudioRecorder does not save fileAVAudioRecorder 不保存文件
【发布时间】:2013-11-17 20:07:17
【问题描述】:

我现在正在努力使用 AVAudioRecorder(和播放器)。我有一个应用程序,其中有三个简单的按钮可以录制/播放/停止通话,我正在使用 AVAudioRecorder 和 AVAudioPlayer 来录制和播放声音。现在,当我测试应用程序(在 iPhone 和模拟器中)时,我得到了 Cocoa 260 - 尝试播放我刚刚录制的内容时找不到文件异常。打印 AVAudioRecorder 的文件 URL 的路径时,我得到了一个文件夹内的文件的 URL,该文件夹在我的计算机上不存在(在模拟器上运行。我的代码如下所示:

- (void)viewDidLoad
{
    [super viewDidLoad];

    _plyBtn.enabled = NO;
    _stopBtn.enabled = NO;

    NSArray *dirPaths;
    NSString *docsDir;

    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    docsDir = dirPaths[0];

    NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"test.caf"];

    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

    NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithInt:AVAudioQualityMin],
                                    AVEncoderAudioQualityKey,
                                    [NSNumber numberWithInt:16],
                                    AVEncoderBitRateKey,
                                    [NSNumber numberWithInt:2],
                                    AVNumberOfChannelsKey,
                                    [NSNumber numberWithFloat:44100.0],
                                    AVSampleRateKey,
                                    nil];

    NSError *error = nil;
    _rec = [[AVAudioRecorder alloc] initWithURL:soundFileURL settings:recordSettings error:&error];

    if(error){
        NSLog(@"error: %@", [error localizedDescription]);
    } else {
        [_rec prepareToRecord];
    }

    NSLog(@"path: %@", soundFileURL);
}

我不知道为什么底部的 NSLog 会打印无效的文件路径...就像我说的,我在录制时没有收到错误,但是在尝试播放我之后录制的音频时。这是我的播放功能:

- (IBAction)playAudio:(id)sender {
    if(!_rec.recording){
        _recBtn.enabled = NO;
        _stopBtn.enabled = YES;

        NSError *error1;
        NSError *error2;

        NSData *soundFile = [[NSData alloc] initWithContentsOfURL:_rec.url options:NSDataReadingMappedIfSafe error:&error1];
        _ply = [[AVAudioPlayer alloc] initWithData:soundFile error:&error2];

        //_ply = [[AVAudioPlayer alloc] initWithContentsOfURL:_rec.url error:&error];

        _ply.delegate = self;

        if(error1){
            NSLog(@"error: %@", [error1 localizedDescription]);
        } else if (error2) {
            NSLog(@"error: %@", [error2 localizedDescription]);
        } else {
            [_ply play];
        }
    }
}

这似乎不起作用:

- (IBAction)recordAudio:(id)sender {
    NSLog(@"recordAudio called");
    _plyBtn.enabled = NO;
    _stopBtn.enabled = YES;
    [_rec record];
}

提前感谢您的帮助:)

【问题讨论】:

    标签: ios objective-c avaudioplayer avaudiorecorder file-not-found


    【解决方案1】:

    可能录制路径无效。

    NSDocumentationDirectory...不应该是 NSDocumentDirectory 吗?

    【讨论】:

    • 就是这样!谢谢!自动补全就这么多:)
    • err.. 那不应该是自动完成吗? ^.^
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    相关资源
    最近更新 更多