使用AVAudioRecorder看似随机文件损坏(有时文件无法播放) - iOS [英] Seemingly random file corruption using AVAudioRecorder (Sometimes the file can't be played back) - iOS

查看:150
本文介绍了使用AVAudioRecorder看似随机文件损坏(有时文件无法播放) - iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在开发的应用程序中,我或多或少地碰到了一堵砖墙。在应用程序中,您可以输入一个视图,该视图列出标准表视图中所有本地保存的音频文件。从这里你可以点击它们来播放它们,或点击下面的录音按钮进行新的录音,然后自动保存到应用程序沙箱。

In an app I'm currently developing, I've more or less hit a brick wall. In the application you can enter a view which lists all locally saved audio files in a standard table view. From here you can click on them to play them, or hit a record button below to make a new recording, which will afterwards automatically get saved to the apps sandbox.

现在所有这些在大多数情况下实际上都非常好。我可以坐下来录制并播放它们。我可以坐下来测试这个,大约45岁,没有任何问题。然后突然在随机的时候我会遇到一个非常奇怪的问题。问题是录音机突然开始只保存无法播放的文件,无论你录制多长时间都无法播放,大小只有4096字节。

Now all of this actually works perfectly fine, most of the time. I can sit and make recordings and play them back. I can sit and test this on and on for around 45 with no problems whatsoever. Then suddenly at random times I will hit a very odd problem. The problem is that the recorder all of the sudden starts to save nothing but corrupt files which can't be played back and are exactly 4096 bytes in size, no matter how long you recorder for.

我以完全标准的方式使用AVAudioRecorder,设置如下:

I use the AVAudioRecorder in a totally standard fashion, setting it up like this:

// Specific settings for the audio recording
NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithInt:AVAudioQualityMin],
    AVEncoderAudioQualityKey,
    [NSNumber numberWithInt:8], 
    AVEncoderBitRateKey,
    [NSNumber numberWithInt: 2], 
    AVNumberOfChannelsKey,
    [NSNumber numberWithFloat:22000.0], 
    AVSampleRateKey,
    nil];

NSError *error = nil;
audioRecorder = [[AVAudioRecorder alloc]
    initWithURL:contentURL
    settings:recordSettings
    error:&error];
audioRecorder.delegate = self;

我开始这样的录音:

if (!isRecording) {
    isRecording = YES;
    [audioRecorder record];

    // Start the timer
    recordTiming = [NSDate date];

    timeCheck = [NSTimer
        scheduledTimerWithTimeInterval:1.0f
        target:self
        selector:@selector(timeCheck)
        userInfo:nil
        repeats:YES];
}

然后用这个停止它:

if (isRecording) {
    [audioRecorder stop];

    // Stop the timer
    [timeCheck invalidate];
}

我不知道是什么原因造成这个问题。到目前为止,我几乎尝试过所有事情。我已经检查确保记录器对象被正确回收(所以你现在每个记录都有一个新的实例),并且旧的引用被解除分配等等,以确保它与干扰对象无关,但没有解决它。

I have no idea what could cause this problem to happen. So far I've tried almost everything. I've check to make sure that the recorder object is recycled properly (so you have a new instance for each now recording), and that the old reference is deallocated and so on, to make sure it has nothing to do with interfering objects, but nothing solves it.

有没有人对可能导致文件损坏的问题有所了解?

Is there anyone who have a slight idea of what could cause problems with corrupted files?

推荐答案

注意:由于OP暂时无效,我想这个问题应该回答公众未来的参考,因此我发布了这个答案。

必须在开始录制之前获得 AVAudioSession

The AVAudioSession has to be obtained before start recording.

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *err = nil;
[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
if(err){
   NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
   return;
}
err = nil;
[audioSession setActive:YES error:&err];
if(err){
   NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
   return;
}

参考:https://stackoverflow.com/a/9706731/188331

这篇关于使用AVAudioRecorder看似随机文件损坏(有时文件无法播放) - iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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