2011-08-09 70 views
6

我試圖同時錄製和播放視頻。這可能與基礎?目前我能夠做到這一點,只要我不記錄音頻。只要我添加到AVCaptureSession的音頻輸入,並重新啓動我收到「AVCaptureSessionWasInterruptedNotification」,並停止錄製的整個事情。Avfoundation - 同時播放和錄製視頻(以及音頻和預覽)

這就是我如何播放視頻。

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]; 

而且我這是怎麼錄製視頻:凡爲我用 「AVCamCaptureManager」 從蘋果AVCam示例代碼

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]; 
} 

回答

4

首先,設置moviePlayer使用的應用程序音頻會話:調用

moviePlayer.useApplicationAudioSession=YES; 

然後,前[captureManager會議] startRunning],激活其類別設置音頻會話「戲並記錄「並覆蓋其屬性以允許它與其他人混合。

// 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]); 
} 
+0

這很有趣,因爲它與此處接受的答案相矛盾:http://stackoverflow.com/a/10559420/954643它說在捕獲會話後啓動音頻會話。 – qix

+0

實際上,文檔對'useApplicationAudioSession'這樣說:「一個布爾值,指示電影播放器​​是否應該使用該應用的音頻會話(iOS 6.0中不推薦使用該屬性,並且不鼓勵使用該屬性)。」默認值已經是'YES'。 – qix

0

只是其他人是否知道...我得到了上面的工作方式以及該Wertesse提出,但它也可以用AVPlayer(不具備useApplicationAudioSession屬性)。

+0

Hey Bernt,有沒有關於如何讓這個工作的更多見解?我在viewDidLoad函數中有效地複製了原始問題中的代碼,並添加了AudioSession配置。但是,我的CaptureSession仍然停止。代碼示例將不勝感激。謝謝! – kungfoo