2011-08-09 35 views
6

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ã.

Trả lời

4

Thứ nhất, thiết lập các MoviePlayer sử dụng phiên ứng dụng âm thanh:

moviePlayer.useApplicationAudioSession=YES; 

Sau đó, trước gọi [[captureManager phiên] startRunning], kích hoạt một phiên âm thanh với thể loại của nó thiết lập để "chơi và ghi lại "và ghi đè thuộc tính của nó để cho phép nó kết hợp với những người khác.

// 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

Đ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

+0

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

0

Chỉ cần có ai tự hỏi ... Tôi có cách tiếp cận trên để làm việc tốt mà Wertesse đề xuất, nhưng nó cũng làm việc với một AVPlayer (mà không có thuộc tính useApplicationAudioSession).

+0

Hey Bernt, Có thông tin chi tiết bổ sung nào về cách làm việc này không? Tôi đã sao chép mã một cách hiệu quả trong câu hỏi gốc trong hàm viewDidLoad của tôi và tôi đã thêm cấu hình AudioSession. Tuy nhiên, CaptureSession của tôi vẫn đang bị dừng lại. Mẫu mã sẽ được đánh giá cao. Cảm ơn! – kungfoo

Các vấn đề liên quan