次のコードを使用して、AVCaptureSessionを設定し、ビデオファイルを録画して再生します。これで問題なく動作する場合もあれば、再生時に黒い画面が表示される場合もあります。私が知る限り、それは完全にランダムです。
バグが発生したときに、ファイルをQuickTimeで開こうとすると、「ファイルを開くことができません。フォーマットが認識されません」というメッセージが表示されます。これは、再生の問題ではなく、録音の問題であると私に信じさせます。
また、マイク入力を追加するコードの部分をコメントアウトすると、バグは発生しません(ただし、私のビデオファイルにはもちろんオーディオトラックがありません)...したがって、オーディオフィードによってファイルがランダムに破損する可能性があります理由?
- (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」ファイルに次のように記録します。
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];
そして、再生が押されると、私は映画ファイルを次のように再生します:
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];
私はしばらくこれにいて、それを理解することができません、それは本当に非常にランダムです。どんな助けでも大歓迎です!
編集:movie.movファイルがすでに存在する場合は、新しい記録を開始する前に削除します。
EDIT2:この行と関係がある可能性があります:
[movieOutput startRecordingToOutputFileURL:pathUrl recordingDelegate:self];
Xcodeは、この行に「互換性のないタイプ'id'のパラメーターにViewController*const_strong'を送信しています」という警告を表示します
EDIT3:問題は解決しました。まもなく回答を投稿します。ありがとう!