AVCaptureSession的怪异错误 [英] Weird bug with AVCaptureSession

查看:41
本文介绍了AVCaptureSession的怪异错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码来设置AVCaptureSession,记录视频文件并进行播放: 有时,这种方法工作得很好,而其他时候,我在播放时出现黑屏.据我所知,这是完全随机的.

I use the following code to set up an AVCaptureSession, record a video file, and play it back: Sometimes this works just fine, and other times I get a black screen on playback. As far as I can tell it's completely random.

当错误发生时,如果我尝试在快速时间内打开文件,则会收到文件无法打开,格式无法识别"的消息.这使我相信它是一个录制问题,而不是播放问题.

When the bug happens, if I try to open the file in quicktime, i get a "file cannot be opened, format not recognized" message. This leads me to believe its a recording issue rather than a playback issue.

此外,如果注释掉添加麦克风输入的代码部分,则不会发生错误(但是我的视频文件当然没有音轨)...因此,音频馈送会随机破坏文件出于某种原因?

Also, if comment out the part of the code which adds the microphone input, the bug does not occur (but my video file doesn't have an audio track of course)...so maybe the audio feed randomly corrupts the file for some reason?

- (void)viewDidLoad {
[super viewDidLoad];

....

captureSession = [[AVCaptureSession alloc] init];
[captureSession setSessionPreset:AVCaptureSessionPresetHigh];

NSArray *devices = [AVCaptureDevice devices];
AVCaptureDevice *frontCamera;
AVCaptureDevice *mic = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];

for (AVCaptureDevice *device in devices) {

    NSLog(@"Device name: %@", [device localizedName]);

    if ([device hasMediaType:AVMediaTypeVideo]) 
    {
        if ([device position] == AVCaptureDevicePositionFront) {
            NSLog(@"Device position : front");
            frontCamera = device;
        }
    }
}

NSError *error = nil;

AVCaptureDeviceInput * microphone_input = [AVCaptureDeviceInput deviceInputWithDevice:mic error:&error];

AVCaptureDeviceInput *frontFacingCameraDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:frontCamera error:&error];

if (!error) 
{
    if ([captureSession canAddInput:frontFacingCameraDeviceInput])
    {
        [captureSession addInput:frontFacingCameraDeviceInput];
    }

    if([captureSession canAddInput:microphone_input])
    {
        [captureSession addInput:microphone_input];
    }

}
[captureSession startRunning];
}

当按下记录时,我使用以下命令记录到"movie.mov"文件中:

When record is pressed, I record to a "movie.mov" file with:

movieOutput = [[AVCaptureMovieFileOutput alloc]init];
[captureSession addOutput:movieOutput];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pathString = [documentsDirectory stringByAppendingPathComponent:@"movie.mov"];
NSURL *pathUrl = [NSURL fileURLWithPath:pathString];

[movieOutput startRecordingToOutputFileURL:pathUrl recordingDelegate:self];

当按下播放按钮时,我使用以下命令播放电影文件:

And when play is pressed, I play back the movie file with:

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pathString = [documentsDirectory stringByAppendingPathComponent:@"movie.mov"];
NSURL *pathUrl = [NSURL fileURLWithPath:pathString];

mPlayer = [AVPlayer playerWithURL:pathUrl];

[self.video setPlayer:self.mPlayer];
[video setVideoFillMode:@"AVLayerVideoGravityResizeAspectFill"];
[self.mPlayer play];

我已经有一段时间了,无法弄清楚,这确实是非常随机的.感谢您的帮助!

Ive been at this for a while now and can't figure it out, it really is very random. Any help is appreciated!

如果movie.mov文件已经存在,请在开始新录制之前将其删除.

If the movie.mov file already exists, I delete it before starting the new recording.

可能与该行有关: [movieOutput startRecordingToOutputFileURL:pathUrl recordingDelegate:self]; Xcode在此行给我一个将ViewController * const_strong'发送到不兼容类型'id'的参数"警告

Could it have something to do with the line: [movieOutput startRecordingToOutputFileURL:pathUrl recordingDelegate:self]; Xcode gives me a "Sending ViewController *const_strong 'to parameter of incompatible type 'id'" warning on this line

问题已解决,将很快发布答案.谢谢!

Problem solved, will post answer shortly. Thanks!

推荐答案

我想我已经解决了:

AVCaptureMovieFileOutput类具有一个默认为10的movieFragmentInterval变量.因此,每隔10秒钟,就会将一个样本表"写入quicktime文件.没有示例表,该文件将无法读取.

The AVCaptureMovieFileOutput class has a movieFragmentInterval variable which defaults to 10. So every 10 seconds, a "sample table" is written to the quicktime file. Without a sample table the file is unreadable.

当我测试时,我的所有录音都很短,因此似乎每当未将样本表写出到文件时,都会发生该错误.这很奇怪,因为我假设我在调用[movieOutput stopRecording]时会写出示例表,但是我猜没有.当我使用以下命令将fragmentInterval降低到5秒时:

When I tested this all of my recordings were pretty short, so it seems like the bug happened whenever a sample table wasnt written out to the file. That's weird because I assumed when i call [movieOutput stopRecording] that this would write out the sample table, but I guess it didnt. When I lower the fragmentInterval to 5 seconds with:

CMTime fragmentInterval = CMTimeMake(5,1);

[movieOutput setMovieFragmentInterval:fragmentInterval];
[movieOutput startRecordingToOutputFileURL:pathUrl recordingDelegate:self];

该错误似乎已消失.我仍然不确定为什么仅在将麦克风添加为输入设备时才发生错误.

The bug seems to have dissapeared. Im still unsure why the error only happened whenever the mic was added as an input device though.

这篇关于AVCaptureSession的怪异错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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