美文网首页
视频录制

视频录制

作者: 流星大石头 | 来源:发表于2016-04-23 13:07 被阅读135次
    视频录制需要导入AVFoundation框架,视频录制的基本步骤:

    <1>创建视频、音频设备
    <2>指定视频、音频设备为输入设备
    <3>创建视频文件输出对象
    <4>创建会话对象
    <5>通过会话对象关联输入和输出
    <6>创建特殊的Layer,添加到self.view.layer,用来展示捕捉到的画面,可以自定义这个Layer,自定义录制界面
    <7>开启会话
    <8>开始录制
    <9>停止录制

    #import@interface ViewController ()
    /**
    
    *  视频输入设备
    
    */
    
    @property (nonatomic,strong) AVCaptureDeviceInput* inputPicture;
    
    /**
    
    *  音频输入设备
    
    */
    
    @property (nonatomic,strong) AVCaptureDeviceInput* inputAudio;
    
    /**
    
    *  输出
    
    */
    
    @property (nonatomic,strong) AVCaptureMovieFileOutput* output;
    
    /**
    
    *  会话 关联输如输出
    
    */
    
    @property (nonatomic,strong) AVCaptureSession* session;
    
    /**
    
    *  特殊的Layer,用来 展示录制的画面,可以自定义这个画面,展示自定义的录制界面
    
    */
    
    @property (nonatomic,strong) AVCaptureVideoPreviewLayer* preLayer;
    
    @property (nonatomic,strong) UIButton* recordButton;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
    
    [super viewDidLoad];
    
    //1. 创建视频、音频设备
    
    AVCaptureDevice* inputPicture = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    AVCaptureDevice* inputAudio = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
    
    //2.指定视频、音频设备为输入设备
    
    self.inputPicture = [AVCaptureDeviceInput deviceInputWithDevice:inputPicture error:nil];
    
    self.inputAudio = [AVCaptureDeviceInput deviceInputWithDevice:inputAudio error:nil];
    
    //3. 创建视频文件输出
    
    self.output = [[AVCaptureMovieFileOutput alloc] init];
    
    //4. 创建会话,
    
    self.session = [[AVCaptureSession alloc] init];
    
    //5. 关联输入输出设备
    
    if ([self.session canAddInput:self.inputAudio]) {
    
    [self.session addInput:self.inputAudio];
    
    }
    
    if([self.session canAddInput:self.inputPicture]){
    
    [self.session addInput:self.inputPicture];
    
    }
    
    if([self.session canAddOutput:self.output]) {
    
    [self.session addOutput:self.output];
    
    }
    
    //6. 创建特殊的Layer添加到view的layer上面,展示捕捉到的画面,可以自定Layer,定制录制时的画面
    
    self.preLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
    
    self.preLayer.frame = self.view.bounds;
    
    [self.view.layer addSublayer:self.preLayer];
    
    //7. 开启会话
    
    [self.session startRunning];
    
    self.recordButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    
    self.recordButton.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2,[UIScreen mainScreen].bounds.size.height-100);
    
    self.recordButton.titleLabel.font = [UIFont systemFontOfSize:25];
    
    [self.recordButton setTitle:@"录 制" forState:UIControlStateNormal];
    
    [self.recordButton setTitle:@"暂 停" forState:UIControlStateSelected];
    
    [self.recordButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [self.view addSubview:self.recordButton];
    
    [self.recordButton addTarget:self action:@selector(recordVideo) forControlEvents:UIControlEventTouchUpInside];
    
    }
    
    - (void)recordVideo{
    
    //9. 停止录制
    
    if ([self.output isRecording]) {
    
    self.recordButton.selected = NO;
    
    [self.output stopRecording];
    
    }
    
    else {
    
    //8. 开始录制
    
    self.recordButton.selected = YES;
    
    NSString* path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"hel.mp4"];
    
    [self.output startRecordingToOutputFileURL:[NSURL fileURLWithPath:path] recordingDelegate:self];
    
    }
    
    }
    
    - (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error {
    
    NSLog(@"已经录制完成");
    
    }
    

    相关文章

      网友评论

          本文标题:视频录制

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