Record Audio/Video with AVCaptureSession and Playback Audio simultaneously?

独自空忆成欢 提交于 2020-01-01 01:16:52

问题


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

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.

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

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 a workaround for this?

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

mc.


回答1:


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

Therefore you can play BGM

    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:music_url error:nil];
    [self.player setDelegate:self];
    [self.player play];

and record the movie

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


来源:https://stackoverflow.com/questions/9169936/record-audio-video-with-avcapturesession-and-playback-audio-simultaneously

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!