MediaPlayer视频录制

2016-07-09  本文已影响339人  e85a0a8a9ba4

创建图像选择器

//1. 判断是否可用
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        return;
    }
    
    //2. 创建图像选择器
    UIImagePickerController *picker = [UIImagePickerController new];
    
    //3. 设置类型
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    
    //4. 设置媒体类型
    picker.mediaTypes = @[(NSString *)kUTTypeMovie];
    
    //5. 设置相机检测模式
    picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
    
    //6. 设置视频的质量
    picker.videoQuality = UIImagePickerControllerQualityTypeHigh;
    
    //7. 设置代理
    picker.delegate = self;
    
    //8. 模态弹出
    [self presentViewController:picker animated:YES completion:nil];

处理代理方法 保存视频

//UIImagePickerController 代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    
    //1. 获取媒体类型
    NSString *mediaTyep = info[UIImagePickerControllerMediaType];
    
    //2. 判断是否是视频的媒体类型
    
    id url = info[UIImagePickerControllerMediaURL];
    
    if ([mediaTyep isEqualToString:(NSString *)kUTTypeMovie]) {
        if (self.mpC == nil) {
            self.mpC = [MPMoviePlayerController new];
            self.mpC.view.frame = self.view.bounds;
            [self.view addSubview:self.mpC.view];
        }
        self.mpC.contentURL = url;
        [self.mpC play];
    }
    
    //3. 保存视频
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        
        //3.1 创建ALAssetsLibrary对象
        ALAssetsLibrary *assetsLibrary = [ALAssetsLibrary new];
        
        //3.2 调用writeVideoAtPathToSavedPhotosAlbum即可
        //前面的URL, 需要传入要保存的视频的URL.
        [assetsLibrary writeVideoAtPathToSavedPhotosAlbum:url completionBlock:nil];
        
    }
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}
上一篇下一篇

猜你喜欢

热点阅读