iOS自定义视频录制界面

作者: Geek_zheng | 来源:发表于2016-01-02 14:35 被阅读1579次

    相信在iOS应用里面很常用的功能之一就是视频录制了,但是往往系统的录制界面不是我们想要的,所以这时候就得自定义视频录制界面了.当然对很多人来讲的,AVFoundation这个库不是很熟.下面就一步步来实现一个自定义界面把!

    创建UI以及相机采集工作

    NSError *error = nil;
            self.session = [[AVCaptureSession alloc] init];
    //可以根据自己的需求来决定预览层的画质
            _session.sessionPreset = AVCaptureSessionPresetLow;
            AVCaptureDevice *frontCamera = nil;
            AVCaptureDevice *backCamera = nil;
            NSArray *cameras = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
            for (AVCaptureDevice *camera in cameras) {
                if (camera.position == AVCaptureDevicePositionFront) {
                    frontCamera = camera;
                } else {
                    backCamera = camera;
                }
            }
            if (!backCamera) {
                self.isCameraSupported = NO;
                return;
            } else {
                self.isCameraSupported = YES;
                if ([backCamera hasTorch]) {
                    self.isTorchSupported = YES;
                } else {
                    self.isTorchSupported = NO;
                }
            }
            if (!frontCamera) {
                self.isFrontCameraSupported = NO;
            } else {
                self.isFrontCameraSupported = YES;
            }
            [backCamera lockForConfiguration:nil];
            if ([backCamera isExposureModeSupported:AVCaptureExposureModeContinuousAutoExposure]) {
                [backCamera setExposureMode:AVCaptureExposureModeContinuousAutoExposure];
            }
            [backCamera unlockForConfiguration];
            // 用device对象创建一个设备对象input,并将其添加到session
            self.videoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:backCamera error:&error];
            if (!_videoDeviceInput) {
                return;
            }
            [_session addInput:_videoDeviceInput];
            
            AVCaptureDeviceInput *audioDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio] error:nil];
            if (!audioDeviceInput) {
            }
            _output = [[AVCaptureVideoDataOutput alloc] init];
            [_session addOutput:_output];
            
    
            dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
            [_output setSampleBufferDelegate:self queue:queue];
            //这里作者我需要的是480*480的视频  所有先取640x480,后期通过裁剪来获取到480*480的   画质越高处理的速度相对较慢,所以根据自己的需求来定义
            _session.sessionPreset = AVCaptureSessionPreset640x480;
            
            dispatch_async(dispatch_get_main_queue(), ^{
                self.preViewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
                _preViewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
                [_session commitConfiguration];
                // 启动session
                 [_session startRunning];
                
                self.preViewLayer.frame = CGRectMake(0, 0, MYSCREENWIDTH, MYSCREENWIDTH*4/4);
    //preview是一个UIView,用来加载相机的预览层
                [preview.layer addSublayer:self.preViewLayer];
                
            });
    

    获取视频流

    #pragma mark 获取视频流
    
    - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
    {
            //在这里你可以获取到视频采集到的视频帧音频帧,具体要做什么操作  旋转啊  裁剪啊 之类的    就可以根据项目需求来决定
    }
    

    相关文章

      网友评论

      • 禅牧:作者有没有demo参考一下 751185567@qq.com,麻烦发我一份,谢谢
      • 84d8724e256d:视频裁剪帮帮忙
        Geek_zheng:@84d8724e256d 哪里不懂请留言
      • puppySweet:你截取的尺寸是480x480但是你的分辨率还是640x 480 这不是变形了么 能不能一开始设置480x480
        Geek_zheng:@puppySweet 不能,因为相机分辨率尺寸就那么几种,不会变形,你只需要裁剪你需要的那一部分
      • puppySweet:这个框架能处理视频的尺寸么……控制播放速率和帧数
      • 7b77e6e820c6:好像没作用

      本文标题:iOS自定义视频录制界面

      本文链接:https://www.haomeiwen.com/subject/qvnwhttx.html