Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playing audio file while recording video in iOS

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);
like image 906
Batu Avatar asked Feb 20 '13 11:02

Batu


1 Answers

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

like image 165
C0D3 Avatar answered Sep 20 '22 18:09

C0D3