同时使用AVCaptureSession和播放音频录制音频/视频?

时间:2012-02-07 01:41:16

标签: objective-c ios audio avfoundation avcapturesession

我正在尝试创建一个可以录制音频和视频的iOS应用,同时将音频输出到扬声器。

要进行录制和预览,我使用AVCaptureSession,视频和音频分别为AVCaptureConnection,视频和音频分别为AVAssetWriterInput。我基本上通过遵循RosyWriter示例代码实现了这一点。

在以这种方式设置录制之前,我使用AVAudioPlayer播放音频。

现在,如果我处于捕获过程中(甚至不是录制,只是捕获预览),并尝试使用AVAudioPlayer,我的captureOutput课程上的AVCaptureAudioDataOutputSampleBufferDelegate回调会停止被叫。

有解决方法吗?

我应该使用另一种较低级别的服务来播放不会阻止我捕获的音频吗?

MC。

1 个答案:

答案 0 :(得分: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];
相关问题