iOS record video and play MP3 at the same time

1.5k Views Asked by At

I need to record video from the iPhone camera, and play an MP3 file at the same time.

I started with AVCam sample code, which I'm sure you all have. It works great for recording video.

However, I then added the following code to play an MP3. This MP3-playing code works in a different app of mine, but when I insert it into this sample code the MP3 not only does not play, the AVCamCaptureManager's recordingDidFinishToOutputFileURL never gets called, so the video never gets saved out.

It's like the audio playing code conflicts with the video capture code. Any ideas?

Here's the audio playing code I put into AVCam:

 AVAudioPlayer *audioPlayer = nil;
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/soundeffect.mp3", [[NSBundle mainBundle] resourcePath]]];
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    [audioPlayer setVolume:1.0]; 
    [audioPlayer prepareToPlay];
    [m_audioPlayer play];

Pretty simple code here. Not sure why this audio playing code causes recordingDidFinishToOutputFileURL to never get called...

Thanks for any ideas.

1

There are 1 best solutions below

1
On

I figured it out: I just had to remove some code within AVCam that allocated AVCaptureDeviceInput - audioInput. That was unnecessary and conflicted with my audio playback code.