同时使用AVCaptureSession和播放音频录制音频/视频? [英] Record Audio/Video with AVCaptureSession and Playback Audio simultaneously?

查看:249
本文介绍了同时使用AVCaptureSession和播放音频录制音频/视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create an iOS app which can record audio and video while simultaneously outputting audio to the speakers.

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

To do the recording and preview, I'm using AVCaptureSession, an AVCaptureConnection each for both video and audio, and an AVAssetWriterInput each for both video and audio. I basically achieved this by following the RosyWriter example code.

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

Prior to setting up recording in this fashion, I was using AVAudioPlayer to play audio.

现在,如果我正在捕捉(甚至没有录制,只是捕捉预览),并尝试使用 AVAudioPlayer ,my captureOutput 回调我的 AVCaptureAudioDataOutputSampleBufferDelegate 类停止调用。

Now, if I am in the middle of capturing (not even recording, just capturing for preview), and attempt to use AVAudioPlayer, my captureOutput callbacks on my AVCaptureAudioDataOutputSampleBufferDelegate class stop getting called.

这是否有解决方法?

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

Is there another, lower level service I should use to be playing audio that won't stop my capture?

mc。

推荐答案

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

You first set the AVAudioSession and its category. For example in viewDidLoad method,

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

这篇关于同时使用AVCaptureSession和播放音频录制音频/视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆