iphone - dev - sdk de apple
Avfoundation: reproduce y graba video(junto con audio y vista previa) simultáneamente (2)
Intento grabar y reproducir video simultáneamente. ¿Es esto posible con avfoundation? Actualmente puedo hacerlo siempre que no grabe audio. Tan pronto como agregue la entrada de audio a AVCaptureSession y reinicie todo, recibo "AVCaptureSessionWasInterruptedNotification" y la grabación se detiene.
Así es como juego 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];
Y así es como grabo 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];
}
Donde estoy usando "AVCamCaptureManager" del código de muestra AVCam de Apple.
Primero, configure moviePlayer para usar la sesión de audio de la aplicación:
moviePlayer.useApplicationAudioSession=YES;
Luego, antes de llamar a [[session de captureManager] startRunning] , active una sesión de audio con su categoría establecida para "reproducir y grabar" y anule su propiedad para permitir que se mezcle con otras.
// Set audio session category to "play and record"
NSError* error = nil;
AVAudioSession* audioSession = [AVAudioSession sharedInstance];
if (![audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error]) {
NSLog(@"AVAudioSession setCategory failed: %@", [error localizedDescription]);
}
// Set audio session property "allow mixing" to true so audio can be recorded while it is playing
UInt32 allowMixing = true;
OSStatus status = AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(allowMixing), &allowMixing);
if (status != kAudioSessionNoError) {
NSLog(@"AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers) failed: %ld", status);
}
// Activate the audio session
error = nil;
if (![audioSession setActive:YES error:&error]) {
NSLog(@"AVAudioSession setActive:YES failed: %@", [error localizedDescription]);
}
Solo si alguien más se está preguntando ... también conseguí que el enfoque anterior funcionara, lo propuso Wertesse, pero también funciona con un AVPlayer (que no tiene el atributo useApplicationAudioSession).