使用AVCaptureSession和AVAudioPlayer合并运用

10
我能够录制视频并正确地将电影输出到文件。然而,当使用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];
}
1个回答

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

嗨,HappyAppDeveloper,我遇到了同样的问题。我做的事情与您相同,在setupAudioSession中进行相同的初始化,但我的捕获会话总是停止。您知道这是否适用于iOS6吗? - kungfoo
如果您正在使用ARC,则必须将音频播放器声明为“strong”。否则,它可能会被过早释放,您将无法听到任何声音。 - Sten
谢谢,它起作用了。我还必须将AudioToolbox框架添加到我的项目中。 - Ravi_Parmar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接