2011-08-09 28 views
6

ビデオを同時に録画して再生しようとしています。これはavfoundationで可能ですか?現在、私はオーディオを録音しない限り、それを行うことができます。オーディオ入力をAVCaptureSessionに追加して全体を再起動すると、AVCaptureSessionWasInterruptedNotificationが受信され、録音が停止します。概要 - ビデオを再生して同時に録画する(オーディオとプレビューを同時に)

これは私がビデオを再生する方法です。

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

そして、これはどのように記録するビデオです:私はリンゴAVCamサンプルコードから "AVCamCaptureManager" を使用していますよう

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

。 、そして、

moviePlayer.useApplicationAudioSession=YES; 

[[captureManagerセッション] startRunning]を呼び出し、「遊びに設定されたカテゴリがオーディオセッションをアクティブ化:

答えて

4

まず、アプリケーションのオーディオセッションを使用するのMoviePlayerを設定しましたそれを他の人と混在させるためにプロパティをオーバーライドします。

// 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が提案されていることを仕事に上記のアプローチを得たが、それはまた、(useApplicationAudioSession属性を持っていない)AVPlayerで動作します。

+0

Hey Bernt、これを動作させる方法に関する追加の洞察はありますか?私は効果的に私のviewDidLoad関数の元の質問のコードを複製し、私はAudioSessionの設定を追加しました。しかし、私のCaptureSessionはまだ停止しています。コードサンプルは非常に高く評価されます。ありがとう! – kungfoo

関連する問題