在其他video正在播放时录制video

我正在使用UIImagePickerController来录制video。 并使用AVPlayer播放video。 并添加AVPlayerLayerUIImagePickerController's cameraOverlayView以便我可以在录制时看到video。 我的要求是

  1. 我需要在使用UIImagePickerController录制video的同时观看video
  2. 使用耳机,我需要从播放video听audio
  3. 需要录制我的声音录制video
  4. 只有我的声音应该被logging,但不能播放video的audio。

所有的东西都在工作,但是4.播放video的audio也与我的声音混合 如何处理这种情况? 我的最终目标是

  1. 出场的video是耳机
  2. 录音的input是耳麦的麦克风

请帮我完成这件事。

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

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

因为您正在使用UIImagePickerController进行录制,所以您对扬声器和麦克风没有太多的控制。 所以testing一下,看它是否工作。

如果你仍然有问题,我build议你使用AVCaptureSession录制video没有audio。 看看这个例子如何使用它record-video-with-avcapturesession-2 。

更新:在我的VOIP应用程序中,我使用AVAudioUnit在播放时录制。 所以我觉得唯一的办法就是分开录制video和audio,然后用AVComposition将它组合成一个单一的电影。 使用AVCaptureSession只录制video并使用EZAudio录制audio。 EZAudio使用AVAudioUnit进行logging,以便它能正常工作。 您可以在播放电影时通过录制audio来testing它,看看它是否有效。 我希望这会有所帮助

更新:我testing过,如果你使用耳机或select麦克风回来只工作。 这是testing代码:

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

看看PBJVision库 。 它允许您在观看预览的同时录制video,最后,您可以随意使用audio和video片段进行任何操作。