Tôi đang cố gắng ghi lại và phát video cùng một lúc. Điều này có thể xảy ra với avfoundation? Hiện tại tôi có thể làm điều đó miễn là tôi không ghi lại âm thanh. Ngay sau khi tôi thêm đầu vào âm thanh vào AVCaptureSession và khởi động lại toàn bộ điều tôi nhận được "AVCaptureSessionWasInterruptedNotification" và dừng ghi âm.Avfoundation - Phát và quay video (cùng với âm thanh và xem trước) đồng thời
Đây là cách tôi phát video.
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
[moviePlayer.view setFrame:self.playerView.bounds];
moviePlayer.useApplicationAudioSession=NO;
self.player = moviePlayer;
[moviePlayer release];
[self.playerView addSubview:player.view];
[player play];
Và đây là cách tôi ghi lại video:
NSError *error;
AVCamCaptureManager *captureManager = [[AVCamCaptureManager alloc] init];
if ([captureManager setupSessionWithPreset:AVCaptureSessionPresetLow error:&error])
{
[self setCaptureManager:captureManager];
AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:[captureManager session]];
self.captureVideoPreviewLayer= previewLayer;
UIView *view = [self cameraView];
CALayer *viewLayer = [view layer];
[viewLayer setMasksToBounds:YES];
CGRect bounds = [view bounds];
[captureVideoPreviewLayer setFrame:bounds];
if ([captureVideoPreviewLayer isOrientationSupported])
[captureVideoPreviewLayer setOrientation:AVCaptureVideoOrientationPortrait];
[captureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[[captureManager session] startRunning];
[self setCaptureVideoPreviewLayer:captureVideoPreviewLayer];
if ([[captureManager session] isRunning])
{
[captureManager setOrientation:AVCaptureVideoOrientationPortrait];
[captureManager setDelegate:self];
[viewLayer insertSublayer:captureVideoPreviewLayer below:[[viewLayer sublayers] objectAtIndex:0]];
NSString *countString = [[NSString alloc] initWithFormat:@"%d", [[AVCaptureDevice devices] count]];
NSLog(@"Device count: %@",countString);
} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Failure"
message:@"Failed to start session."
delegate:nil
cancelButtonTitle:@"Okay"
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Input Device Init Failed"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"Okay"
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
[captureManager release];
if (![[self captureManager] isRecording]) {
[[self captureManager] startRecording];
}
Trong trường hợp như tôi đang sử dụng "AVCamCaptureManager" từ táo AVCam mẫu mã.
Điều này thật thú vị, vì nó mâu thuẫn với câu trả lời được chấp nhận ở đây: http://stackoverflow.com/a/10559420/954643 cho biết để bắt đầu phiên âm thanh sau phiên chụp. – qix
Trên thực tế, các tài liệu nói về 'useApplicationAudioSession':" Giá trị Boolean cho biết trình phát phim có nên sử dụng phiên âm thanh của ứng dụng hay không. Không được thay thế cho thuộc tính này và việc sử dụng nó không được khuyến khích. " Giá trị mặc định là 'CÓ'. – qix