objective-c - 使用 AVFoundation 捕获视频时崩溃

标签 objective-c ios xcode avfoundation

我正在尝试使用 AVFoundation 在我的应用中实现视频捕获。我在 viewDidLoad 下有以下代码:

session = [[AVCaptureSession alloc] init];
movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
videoInputDevice = [[AVCaptureDeviceInput alloc] init];
AVCaptureDevice *videoDevice = [self frontFacingCameraIfAvailable];

if (videoDevice)
{
    NSError *error;

    videoInputDevice = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];

    if (!error)
    {
        if ([session canAddInput:videoInputDevice])
            [session addInput:videoInputDevice];
        else 
            NSLog (@"Couldn't add input.");
    }

}


AVCaptureDevice *audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
NSError *audioError = nil;
AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevice error:&audioError];

if (audioInput)
{
    [session addInput:audioInput];
}

movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];

Float64 TotalSeconds = 35;          //Total seconds
int32_t preferredTimeScale = 30;    //Frames per second
CMTime maxDuration = CMTimeMakeWithSeconds(TotalSeconds, preferredTimeScale);   
movieFileOutput.maxRecordedDuration = maxDuration;

movieFileOutput.minFreeDiskSpaceLimit = 1024 * 1024;                        

if ([session canAddOutput:movieFileOutput])
    [session addOutput:movieFileOutput];

[session setSessionPreset:AVCaptureSessionPresetMedium];
if ([session canSetSessionPreset:AVCaptureSessionPreset640x480])        //Check size based configs are supported before setting them
    [session setSessionPreset:AVCaptureSessionPreset640x480];

[self cameraSetOutputProperties];


[session startRunning];

此代码用于启动捕获的按钮,除此之外:

NSString *outputPath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"output.mov"];
NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:outputPath])
{
    NSError *error;
    if ([fileManager removeItemAtPath:outputPath error:&error] == NO)
    {
        //Error - handle if requried
    }
}
[outputPath release];
//Start recording
[movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self];
[outputURL release];

当我尝试在设备上运行它时,当我尝试加载所有这些都应该发生的 View 时它崩溃了。 Xcode 给我一个“Thread 1: EXC_BAD_ACCESS (code=1, address=0x4) at:

AVFoundation`-[AVCaptureDeviceInput _setDevice:]:
(stuff)
0x3793f608:  ldr    r0, [r1, r0]

错误在最后一行给出。我假设这与某处的 AVCaptureDeviceInput 有关,但我不知道它可能是什么。有谁知道我在这里缺少什么?谢谢。

编辑:在摆弄断点之后,我发现崩溃发生在这一行:

AVCaptureDevice *videoDevice = [self frontFacingCameraIfAvailable];

那么与该方法有关吗?这是我的实现文件,可能那里有问题。

NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
AVCaptureDevice *captureDevice = nil;
for (AVCaptureDevice *device in videoDevices)
{
    if (device.position == AVCaptureDevicePositionFront)
    {
        captureDevice = device;
        break;
    }
}

//  couldn't find one on the front, so just get the default video device.
if ( ! captureDevice)
{
    captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
}

return captureDevice;

编辑 2:可能是我正在使用

AVCaptureDevice *videoDevice = [self frontFacingCameraIfAvailable];

而“ self ”不知何故向其中 throw Spanner ?我知道在创建 CALayer 时可以这样做

CALayer *aLayer = [CALayer layer];

但我不知道与此等效的 AVCaptureDevice,如果有的话。我不确定它可能是什么,从所有方面来看,我的代码似乎都很好,我已经尝试清理项目、重新启动 Xcode、重新启动计算机等。

最佳答案

我很确定问题出在您在模拟器上运行该程序。模拟器无法使用这些资源。

关于objective-c - 使用 AVFoundation 捕获视频时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11214052/

相关文章:

ios - 在 UIView 上创建一个 mask ,将所有 UIImageView subview 着色为黑白

xcode - 从 NSArray 中删除不需要的数据

ios - 如何将特定的 UIImageview 移动到 View 的顶部?

iOS job queue类似于Path的android priority job queue

python - 苹果 API/SDK : requestUserToken without Swift/Objective-C?

ios - 将对象添加到 NSMutableArray 导致崩溃

objective-c - 来自应用程序的新 NSWindow - 不可能完成的任务?

ios - 确定用户是否允许 iOS 7 中的本地通知

c++ - Qt 在寻找匹配项时出现意外的 EOF

ios - 无法从我的应用程序中删除 testflight sdk