2012-02-07 119 views
3

我试图创建一个iOS应用程序,可以记录音频和视频,同时将音频输出到扬声器。同时录制带有AVCaptureSession和回放音频的音频/视频?

要进行录制和预览,我使用AVCaptureSession,AVCaptureConnection分别用于视频和音频,以及各为AVAssetWriterInput用于视频和音频。我基本上是通过遵循RosyWriter示例代码实现的。

此前以这种方式建立记录,我用AVAudioPlayer播放音频。现在

,如果我在拍摄(甚至没有记录,只是捕捉预览)的中间,并尝试使用AVAudioPlayer,我captureOutput回调对我AVCaptureAudioDataOutputSampleBufferDelegate类停止获取调用。

是否有解决方法?

有另一种较低层次的服务,我应该用它来进行播放音频不会停止我的拍摄?

MC。

+0

任何解决这个? – sajwan 2013-01-17 18:54:34

+0

答案:http://stackoverflow.com/a/7219499/2700842 – 2013-12-10 04:28:28

回答

1

您先设置AVAudioSession及其类别。例如,在viewDidLoad方法,

AVAudioSession *session = [AVAudioSession sharedInstance]; 
[session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionDefaultToSpeaker error:nil]; 

因此,你可以玩BGM

self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:music_url error:nil]; 
    [self.player setDelegate:self]; 
    [self.player play]; 

,并记录电影

self.captureSession = [[AVCaptureSession alloc] init]; 
    /* ... addInput AVCaptureDeviceInput AVMediaTypeVideo & AVMediaTypeAudio */ 
    /* ... addOutput */ 
    [self.captureSession startRunning]; 
    AVCaptureMovieFileOutput *m_captureFileOutput =[self.captureSession.outputs objectAtIndex:0]; 
    [m_captureFileOutput startRecordingToOutputFileURL:saveMovieURL recordingDelegate:self];