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];
デバイスで実行しようとすると、これらすべてが発生するはずのビューをロードしようとするとクラッシュします。Xcodeは、次の場所で「スレッド1:EXC_BAD_ACCESS(code = 1、address = 0x4)」を提供します。
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];
そして「自己」はどういうわけかそれにレンチを投げていますか?CALayerを作成するときに、次のことが可能であることを知っています。
CALayer *aLayer = [CALayer layer];
しかし、これに相当するAVCaptureDeviceがある場合は、わかりません。他に何ができるかわかりません。すべてのアカウントで私のコードは問題ないようで、プロジェクトのクリーンアップ、Xcodeの再起動、コンピューターの再起動などを試しました。