2012-06-05 25 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

オーディオセッションを設定することで問題を解決しました。オーディオを再生するオーディオプレーヤーオブジェクトを作成する前に、次の関数を呼び出しました。そのようにして、私はビデオ(オーディオ付き)を録音し、同時にオーディオを再生することができました。

+0

こんにちはHappyAppDeveloper、私はこの同じ問題に固執しています。 setupAudioSessionで同じ初期化を行うのと同じことをしますが、キャプチャセッションは常に停止します。これがiOS6のために壊れたかどうか知っていますか? iOS 5と6で完全に動作する – kungfoo

+0

!私のベーコンを保存しました、ありがとう!!!!!!!!!!! – SpaceDog

+0

ARCを使用している場合、オーディオプレーヤーは「強」と宣言する必要があります。それ以外の場合は早期にリリースされ、音が出ません。 – Sten