简体   繁体   English

在iOS中录制视频时播放音频文件

[英]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. 我使用AVAudioPlayer播放音频和AVCamCaptureManager进行录制。

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.) (我正在研究AVCam的例子。)

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. 如果我使用AudioServicesPlaySystemSound正常而不冻结,但这次它只适用于viewDidLoad。

CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileRef;
    soundFileRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) CFBridgingRetain(@"sound"), CFSTR ("wav"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

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: 我的问题不是用于视频,而是用于在通话中播放声音(voip类型应用程序),这个问题的答案对我有用:

Using vibrate and AVCaptureSession at the same time 同时使用vibrate和AVCaptureSession

Hope setting AVAudioSession category to AVAudioSessionCategoryPlayAndRecord will solve the problem. 希望将AVAudioSession类设置为AVAudioSessionCategoryPlayAndRecord将解决问题。 And also set its category options to AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionMixWithOthers . 并将其类别选项设置为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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM