在播放其他视频时录制视频 [英] Record video while other video is playing

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

问题描述

我正在使用 UIImagePickerController 来录制视频。并使用 AVPlayer 播放视频。并将 AVPlayerLayer 添加到 UIImagePickerController的 cameraOverlayView 以便我可以看到录制时的视频。
我的要求是

I am using UIImagePickerController to record a video. and am using AVPlayer to play a video. and adding AVPlayerLayer to UIImagePickerController's cameraOverlayView so that i can see video while recording. My requirement is


  1. 我需要在使用 UIImagePickerController录制视频时观看视频

  2. 使用耳机我需要通过播放视频来收听音频

  3. 需要将我的声音录制到录制视频

  4. 只有我的声音应该录制但不能播放视频的音频。

  1. I need to watch video while recording video using UIImagePickerController
  2. using headset i need to listen audio from playing video
  3. need to record my voice to recording video
  4. only my voice should be recorded but not playing video's audio.

每件事都有效4.但播放视频的音频也混合用我的声音。如何处理这种情况?我的最终目标是

every thing working but 4. audio from playing video also mix with my voice. how to handle this case? My final goal is


  1. 播放视频的输出是耳机

  2. 录音的输入是耳机麦克风

请帮助我完成这项工作。

Please help me to get this done.

推荐答案

您的要求很有趣。所以你需要同时播放和录音,对吧?
因此,您需要使用类别 AVAudioSessionCategoryPlayAndRecord 初始化音频会话。

Your requirement is interesting. So you need to play and record at the same time, right? So that, you will need to initialize audio session with the category AVAudioSessionCategoryPlayAndRecord.

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];

因为您使用 UIImagePickerController 来记录所以你对扬声器和麦克风没有多少控制权。所以测试一下它是否有效。

Because you are using UIImagePickerController to record so you don't have much control to your speaker and your mic. So test and see if it works.

如果您仍有问题,我建议您使用 AVCaptureSession 录制没有音频的视频。看看这个例子如何使用它 record-video-with-avcapturesession-2

In case you still have problem, I suggest you to use AVCaptureSession to record video without audio. Look at this example how to use it record-video-with-avcapturesession-2.

更新:在我的VOIP应用程序中,我使用 AVAudioUnit 在播放时录制。所以我认为唯一的方法是分别录制视频和音频,然后使用 AVComposition 将其组合成一部电影。使用 AVCaptureSession 仅录制视频并使用 EZAudio 录制音频。 EZAudio 使用 AVAudioUnit 进行记录,以便它可以正常工作。您可以在播放电影时通过录制音频对其进行测试,看看它是否有效。我希望它会有所帮助

UPDATE: In my VOIP application, I use AVAudioUnit to record while playing back. So I think the only way is record video and audio separately and then use AVComposition to compose its to a single movie. Using AVCaptureSession to record video only and use EZAudio to record audio. The EZAudio use AVAudioUnit to record so that it should work. You can test it by record audio while playing a movie and see if it works. I hope it will help

更新:我测试了它,只有你使用耳机或选择麦克风后它才有效。
以下是测试代码:

UPDATE: I tested and it only work if you use headphone or select microphone back. Here is the tested code:

    NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"videoviewdemo" ofType:@"mp4"];
    NSURL *url = [NSURL fileURLWithPath:moviePath];
    // You may find a test stream at <http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8>.
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];

    AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
    AVPlayerLayer *layer = [[AVPlayerLayer alloc] init];
    [layer setPlayer:player];
    [layer setFrame:CGRectMake(0, 0, 100, 100)];
    [self.view.layer addSublayer:layer];

    [player play];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //
        // Setup the AVAudioSession. EZMicrophone will not work properly on iOS
        // if you don't do this!
        //
        AVAudioSession *session = [AVAudioSession sharedInstance];
        NSError *error;
        [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
        if (error)
        {
            NSLog(@"Error setting up audio session category: %@", error.localizedDescription);
        }
        [session setActive:YES error:&error];
        if (error)
        {
            NSLog(@"Error setting up audio session active: %@", error.localizedDescription);
        }

        //
        // Customizing the audio plot's look
        //
        // Background color
        self.audioPlot.backgroundColor = [UIColor colorWithRed:0.984 green:0.471 blue:0.525 alpha:1.0];

        // Waveform color
        self.audioPlot.color = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];

        // Plot type
        self.audioPlot.plotType = EZPlotTypeBuffer;

        //
        // Create the microphone
        //
        self.microphone = [EZMicrophone microphoneWithDelegate:self];

        //
        // Set up the microphone input UIPickerView items to select
        // between different microphone inputs. Here what we're doing behind the hood
        // is enumerating the available inputs provided by the AVAudioSession.
        //
        self.inputs = [EZAudioDevice inputDevices];
        self.microphoneInputPickerView.dataSource = self;
        self.microphoneInputPickerView.delegate = self;

        //
        // Start the microphone
        //
        [self.microphone startFetchingAudio];
        self.microphoneTextLabel.text = @"Microphone On";

        [[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];
    });

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

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