zoukankan      html  css  js  c++  java
  • IOS开发---视频录制

    今天研究了一下使用app录制视频的功能,感觉还是挺简单的。使用了AVFoundation框架,代码比较死,按步骤调用就行。

    分享一下今天做的Demo的步骤


    一,初始化输入设备,这里涉及到前,后摄像头;麦克风(导入AVFoundation)

        //1.创建视频设备(摄像头前,后)
        NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
        //2.初始化一个摄像头输入设备(first是后置摄像头,last是前置摄像头)
        AVCaptureDeviceInput *inputVideo = [AVCaptureDeviceInput deviceInputWithDevice:[devices firstObject] error:NULL];
        
        //3.创建麦克风设备
        AVCaptureDevice *deviceAudio = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
        //4.初始化麦克风输入设备
        AVCaptureDeviceInput *inputAudio = [AVCaptureDeviceInput deviceInputWithDevice:deviceAudio error:NULL];

    二,初始化视频文件输出

        //5.初始化一个movie的文件输出
        AVCaptureMovieFileOutput *output = [[AVCaptureMovieFileOutput alloc] init];
        self.output = output; //保存output,方便下面操作

    三,初始化会话,并将输入输出设备添加到会话中

     1     //6.初始化一个会话
     2     AVCaptureSession *session = [[AVCaptureSession alloc] init];
     3     
     4     //7.将输入输出设备添加到会话中
     5     if ([session canAddInput:inputVideo]) {
     6         [session addInput:inputVideo];
     7     }
     8     if ([session canAddInput:inputAudio]) {
     9         [session addInput:inputAudio];
    10     }
    11     if ([session canAddOutput:output]) {
    12         [session addOutput:output];
    13     }

    四,添加一个视频预览图层,设置大小,添加到控制器view的图层上

    1     //8.创建一个预览涂层
    2     AVCaptureVideoPreviewLayer *preLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
    3     //设置图层的大小
    4     preLayer.frame = self.view.bounds;
    5     //添加到view上
    6     [self.view.layer addSublayer:preLayer];

    五,开始会话

      //9.开始会话 [session startRunning]; 

    六,添加一个按钮:点击开始,停止录制视频,并设置录制视频的代理

     1 - (IBAction)clickVideoBtn:(UIButton *)sender {
     2     //判断是否在录制,如果在录制,就停止,并设置按钮title
     3     if ([self.output isRecording]) {
     4         [self.output stopRecording];
     5         [sender setTitle:@"录制" forState:UIControlStateNormal];
     6         return;
     7     }
     8     
     9     //设置按钮的title
    10     [sender setTitle:@"停止" forState:UIControlStateNormal];
    11     
    12     //10.开始录制视频
    13     //设置录制视频保存的路径
    14     NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"myVidio.mov"];
    15     
    16     //转为视频保存的url
    17     NSURL *url = [NSURL fileURLWithPath:path];
    18     
    19     //开始录制,并设置控制器为录制的代理
    20     [self.output startRecordingToOutputFileURL:url recordingDelegate:self];
    21     
    22 }

    七,实现代理方法(这里只实现一个完成代理方法吧,其他根据自己的需求再设置)

    1 #pragma  mark - AVCaptureFileOutputRecordingDelegate
    2 //录制完成代理
    3 - (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error
    4 {
    5     NSLog(@"完成录制,可以自己做进一步的处理");
    6 }

    八,真机运行,嘿嘿

  • 相关阅读:
    如何给工科生做一个演讲DEMO
    JAVA事务处理系列 值得看
    今天开通了博客,准备开始写点东西或者记录点东西!或者转载点东西!
    屌丝程序员如何打造日PV百万的网站架构
    拥抱大数据时代 DB架构设计
    根据并发请求id查找相应trace信息
    FRM30187: Size of CHAR column in record group must be between 1 and 2000
    10046 SQL trace 的做法
    Oracle 表空间不足的处理办法
    LoadRunner在EBS R12上运行需更改服务器为Socket模式
  • 原文地址:https://www.cnblogs.com/haojuncong/p/ios.html
Copyright © 2011-2022 走看看