支持使用AVCaptureSession进行音频背景录制

时间:2016-04-26 03:40:07

标签: avcapturesession

AVCaptureSession *mSession = [[AVCaptureSession alloc] init];
mSession.sessionPreset=AVCaptureSessionPreset1280x720;
AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
NSError *error = nil;
AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:&error];
if([mSession canAddInput:audioInput]){
    [mSession addInput:audioInput];
    mAudioDataOutput = [[AVCaptureAudioDataOutput alloc] init];
    mAudioDataOutputQueue = dispatch_queue_create("AudioDataOutputQueue", DISPATCH_QUEUE_SERIAL);
    [mAudioDataOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
    if([mSession canAddOutput:mAudioDataOutput]){
        [mSession addOutput:mAudioDataOutput];
    }
}
// make input device
NSError *deviceError;
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *vp = [AVCaptureDeviceInput deviceInputWithDevice:device error:&deviceError];
if([mSession canAddInput:vp]){

    [vp.device lockForConfiguration:nil];
    vp.device.activeVideoMinFrameDuration = CMTimeMake(1, 10);//设置每秒采集帧数
    vp.device.activeVideoMaxFrameDuration = CMTimeMake(1, 30); // 设置每秒采集的最大帧数
    [vp.device unlockForConfiguration];

    // make output device
    mVideoDataOutput = [[AVCaptureVideoDataOutput alloc] init];

    NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey;

    NSNumber* val = [NSNumber
                     numberWithUnsignedInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange];
    NSDictionary* videoSettings =[NSDictionary dictionaryWithObject:val forKey:key];
    mVideoDataOutput.videoSettings = videoSettings;

    [mSession addInput:vp];

    [mSession addOutput:mVideoDataOutput];

    // begin configuration for the AVCaptureSession
    [mSession beginConfiguration];


    AVCaptureConnection *connection = [mVideoDataOutput connectionWithMediaType:AVMediaTypeVideo];

    [connection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];


    if ([connection isVideoStabilizationSupported]) {
        connection.preferredVideoStabilizationMode=AVCaptureVideoStabilizationModeCinematic;
    }
    [mSession commitConfiguration];


    mVideoDataOutputQueue = dispatch_queue_create("VideoDataOutputQueue", DISPATCH_QUEUE_SERIAL);
    [mVideoDataOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];

    mPreviewLayer = [AVCaptureVideoPreviewLayer  layerWithSession:mSession];
    [mPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspect];
    mPreviewLayer.connection.videoOrientation=AVCaptureVideoOrientationLandscapeRight;
    mPreviewLayer.frame = self.view.bounds;
    [self.view.layer addSublayer:mPreviewLayer];

}
[mSession startRunning];

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {

}

应用程序转到后台,我发现AVCaptureVideoDataOutputSampleBufferDelegate AVCaptureAudioDataOutputSampleBufferDelegate无法接收消息。当应用程序转到后台时,AVCaptureSession是否暂停?如果我想在应用程序转到后台时收到消息。我该怎么办?

0 个答案:

没有答案