Record video while other video is playing

2019-03-21 11:08发布

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. 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.

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

  1. Out put for the playing video is headset
  2. Input for the recording is headset's mic

Please help me to get this done.

2条回答
不美不萌又怎样
2楼-- · 2019-03-21 11:08

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

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.

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.

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];
    });
查看更多
干净又极端
3楼-- · 2019-03-21 11:15

Take a look at PBJVision library. It allows you to record video while you are watching the preview, and at the end, you can do whatever you want with audio and video footage.

查看更多
登录 后发表回答