用AVCaptureSession和播放audio同时录制audio/video?
我试图创build一个iOS应用程序,可以录制audio和video,同时向扬声器输出audio。
要进行录制和预览,我使用了AVCaptureSession
, AVCaptureConnection
分别用于video和audio, AVAssetWriterInput
用于video和audio。 我基本上是通过遵循RosyWriter示例代码来实现的。
在以这种方式设置录制之前,我使用AVAudioPlayer
播放audio。
现在,如果我正在捕获(甚至不logging,只是捕获预览),并试图使用AVAudioPlayer
,我的AVCaptureAudioDataOutputSampleBufferDelegate
类的captureOutput
callback停止调用captureOutput
。
有没有解决方法?
是否有另一个低级的服务,我应该用来播放audio,不会停止我的捕获?
MC。
您首先设置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];