在iOS中录制视频时播放音频文件 [英] Playing audio file while recording video in iOS

查看:673
本文介绍了在iOS中录制视频时播放音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用相机录制时播放音频文件。我使用AVAudioPlayer播放音频和AVCamCaptureManager进行录制。

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?

感谢您的帮助。这是代码。 (我正在研究AVCam示例。)

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

这是预览部分。

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

这是音频播放部分。

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

如果我使用AudioServicesPlaySystemSound正常而不冻结,但这次它只适用于viewDidLoad。

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


推荐答案

我的问题不适用于视频,而是适用于在通话中播放声音(voip类型应用程序),这个问题的答案对我有用:

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

这篇关于在iOS中录制视频时播放音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆