2012-06-05 58 views
9

我能夠正確錄製視頻並將影片輸出到文件。但是,在嘗試使用AVAudioPlayer播放某些音頻時,我遇到了視頻錄製問題(無視頻輸出)。這是否意味着我不能同時使用AVCaptureSession和AVAudioPlayer?這裏是我的代碼來創建捕獲會話並播放音頻。視頻捕捉首先啓動,然後在捕捉過程中,我想播放一些音頻。非常感謝您的幫助。一起使用AVCaptureSession和AVAudioPlayer

代碼來創建捕獲會話錄製視頻(帶音頻) - 輸出到.mov文件:

- (void)addVideoInput 
    AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio]; 
    //... also some code for setting up videoDevice/frontCamera 
    NSError *error; 
    AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput 
    deviceInputWithDevice:frontCamera error:&error]; 
    AVCaptureDeviceInput *audioIn = [AVCaptureDeviceInput 
    deviceInputWithDevice:audioDevice error:&error]; 
    if (!error) { 
     if ([captureSession canAddInput:audioIn]) 
       [captureSession addInput:audioIn]; 
     else 
       NSLog(@"Couldn't add audio input."); 
     if ([captureSession canAddInput:videoIn]) 
       [captureSession addInput:videoIn]; 
     else 
       NSLog(@"Couldn't add video input."); 
} 
- (void)addVideoOutput 
{ 
    AVCaptureMovieFileOutput *m_captureFileOutput = [[AVCaptureMovieFileOutput alloc] init]; 
    [captureSession addOutput:m_captureFileOutput]; 

    [captureSession startRunning]; 

    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSMutableString *filePath = [NSMutableString stringWithString:@"Movie.mov"]; 
    NSString* fileRoot = [docDir stringByAppendingPathComponent:filePath]; 
    NSURL *fileURL = [NSURL fileURLWithPath:fileRoot]; 

    AVCaptureConnection *videoConnection = NULL; 

    for (AVCaptureConnection *connection in [m_captureFileOutput connections]) 
    { 
     for (AVCaptureInputPort *port in [connection inputPorts]) 
     { 
      if ([[port mediaType] isEqual:AVMediaTypeVideo]) 
      { 
       videoConnection = connection; 

      } 
     } 
    } 

    [videoConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight]; 


    [m_captureFileOutput startRecordingToOutputFileURL:fileURL recordingDelegate:self]; 
    [m_captureFileOutput release]; 
} 

代碼播放音頻,此功能是視頻採集會話期間調用。如果我沒有調用這個函數,視頻就會被記錄下來,我可以保存到.mov文件中。但是,如果我調用此函數,則不會有輸出.mov文件。

- (void)playAudio 
{ 
    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"AudioFile" ofType:@"mp3"]; 
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath]; 
    AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil]; 
    [fileURL release]; 
    self.audioPlayer = newPlayer; 
    [newPlayer release]; 
    [audioPlayer setDelegate:self]; 
    [audioPlayer prepareToPlay]; 
    [audioPlayer play]; 
} 

回答

19

我通過設置音頻會話解決了這個問題。在創建音頻播放器對象以播放音頻之前,我調用了以下函數。這樣,我就能夠錄製視頻(帶音頻)並同時播放音頻。

- (void)setupAudioSession { 

     static BOOL audioSessionSetup = NO; 
     if (audioSessionSetup) { 
       return; 
     } 
     [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil]; 
     UInt32 doSetProperty = 1; 

     AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty); 

     [[AVAudioSession sharedInstance] setActive: YES error: nil]; 

     audioSessionSetup = YES; 

} 


- (void)playAudio 
{ 
     [self setupAudioSession]; 
     NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"AudioFile" ofType:@"mp3"]; 
     NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath]; 
     AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil]; 
     [fileURL release]; 
     self.audioPlayer = newPlayer; 
     [newPlayer release]; 
     [audioPlayer setDelegate:self]; 
     [audioPlayer prepareToPlay]; 
     [audioPlayer play]; 
    } 
+0

嗨HappyAppDeveloper,我被困在這個同樣的問題。我在setupAudioSession中執行相同的初始化,但我的捕獲會話總是停止。你知道這是否打破了iOS6? – kungfoo

+0

在iOS 5和iOS 6上完美運行!保存我的培根,謝謝!!!!!!!!!!! – SpaceDog

+0

如果您使用ARC,則必須將音頻播放器聲明爲「強大」。否則,它可能會提前發佈,並且您將無法獲得任何聲音。 – Sten