首页 文章

在播放其他视频时录制视频

提问于
浏览
4

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

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

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

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

  • 只应录制我的声音但不播放视频的音频 .

每件事都在工作,但4.播放视频的音频也与我的声音混合在一起 . 如何处理这种情况?我的最终目标是

  • 播放视频的输出是耳机

  • 录音输入是耳机的麦克风

请帮我完成这件事 .

2 回答

  • 0

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

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

    因为您正在使用 UIImagePickerController 进行录制,所以您对扬声器和麦克风没有太多控制权 . 所以测试一下它是否有效 .

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

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

    更新:我测试过,只有你使用耳机或选择麦克风后它才有效 . 这是经过测试的代码:

    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];
        });
    
  • 3

    看看PBJVision library . 它允许您在观看预览时录制视频,最后,您可以使用音频和视频素材执行任何操作 .

相关问题