【问题标题】:Record Audio/Video with AVCaptureSession and Playback Audio simultaneously?使用 AVCaptureSession 录制音频/视频并同时播放音频?
【发布时间】:2012-02-28 12:11:21
【问题描述】:

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

为了进行录制和预览,我使用AVCaptureSessionAVCaptureConnection 用于视频和音频,AVAssetWriterInput 用于视频和音频。我基本上是通过遵循 RosyWriter 示例代码来实现的。

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

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

有解决办法吗?

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

mc.

【问题讨论】:

标签: objective-c ios audio avfoundation avcapturesession


【解决方案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];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-08
    • 2017-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多