6
votes

I want to play audio file while recording with camera. I used AVAudioPlayer for playing audio and AVCamCaptureManager for recording.

But when audio starts to play, preview screen is freezing. What should i do?

Thanks for your helping. Here is the code. (I'm working on AVCam example.)

This is preview part.

if ([self captureManager] == nil) {
        AVCamCaptureManager *manager = [[AVCamCaptureManager alloc] init];
        [self setCaptureManager:manager];

        [[self captureManager] setDelegate:self];

        if ([[self captureManager] setupSession]) {             
            AVCaptureVideoPreviewLayer *newCaptureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:[[self captureManager] session]];
            UIView *view = [self videoPreviewView];
            CALayer *viewLayer = [view layer];
            [viewLayer setMasksToBounds:YES];

            CGRect bounds = CGRectMake(0, 0, 480, 320);
            [newCaptureVideoPreviewLayer setFrame:bounds];

            if ([newCaptureVideoPreviewLayer isOrientationSupported]) {
                [newCaptureVideoPreviewLayer setOrientation:[DeviceProperties setPreviewOrientation]];
            }

            [newCaptureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

            [viewLayer insertSublayer:newCaptureVideoPreviewLayer below:[[viewLayer sublayers] objectAtIndex:0]];

            [self setCaptureVideoPreviewLayer:newCaptureVideoPreviewLayer];
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                [[[self captureManager] session] startRunning];
            });

            [self updateButtonStates];          
        }

And this is audio play part.

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@", sound] ofType:@"wav"]];
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    [audioPlayer play];

If I use AudioServicesPlaySystemSound normal without freezing but this time it only works on viewDidLoad.

CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileRef;
    soundFileRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) CFBridgingRetain(@"sound"), CFSTR ("wav"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileRef, &soundID);
    AudioServicesPlaySystemSound(soundID);
2
Could you post your code?Robert
Any updates on this? I was wondering if you can have the camera on (not necessarily recording video) while recording audio with AVAudioPlayer.Jay Q.
i have also same problem.what do I do now..Arjun Sa

2 Answers

1
votes

My problem wasn't for video but it was for playing a sound while in a call (voip type application) and the answer to this question worked for me:

Using vibrate and AVCaptureSession at the same time

0
votes

Hope setting AVAudioSession category to AVAudioSessionCategoryPlayAndRecord will solve the problem. And also set its category options to AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionMixWithOthers. Please check the below set of code

@property (strong, nonatomic) AVAudioPlayer *audioPlayer;

NSURL *soundUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"VoicemailSound" ofType:@"mp3"]]; //Download and add a system sound to your project and mention its name and type here
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionMixWithOthers error: nil];

[self.audioPlayer prepareToPlay];
[self.audioPlayer play];