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