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

3

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

为了进行录制和预览,我使用了 AVCaptureSession,每个视频和音频都有一个 AVCaptureConnection,每个视频和音频都有一个 AVAssetWriterInput。我基本上是通过遵循 RosyWriter 示例代码来实现的。

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

现在,如果我正在进行捕捉(甚至不是录制,只是为了预览),并且尝试使用 AVAudioPlayer,我的 AVCaptureAudioDataOutputSampleBufferDelegate 类中的 captureOutput 回调将停止调用。

这是否有解决方法?

是否有另一个更低级别的服务可供使用,以播放音频而不会停止捕获?

mc。


答案:https://dev59.com/SWw05IYBdhLWcg3wiyRJ#7219499 - Nick Terry
1个回答

0

首先,您需要设置AVAudioSession及其类别。例如,在viewDidLoad方法中:

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

因此,您可以播放背景音乐

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

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