iOS开发-简单的视频录制和播放

#import "ViewController.h"

#import 

#import 

#import 

@interface ViewController ()

//录像与播放

@property (strong, nonatomic)UIImagePickerController *imgPicker;

@property (strong, nonatomic)AVPlayerViewController *playerCon;

@property (strong, nonatomic)NSURL *url;

@end

@implementation ViewController

-(UIImagePickerController *)imgPicker{

    if (!_imgPicker) {

        _imgPicker=[[UIImagePickerController alloc]init];

        _imgPicker.sourceType=UIImagePickerControllerSourceTypeCamera;

//类型

        _imgPicker.mediaTypes=[NSArray arrayWithObject:(__bridge NSString*)kUTTypeMovie];

//视频质量

        _imgPicker.videoQuality=UIImagePickerControllerQualityTypeHigh;

        _imgPicker.delegate=self;

    }

    return _imgPicker;

}

-(AVPlayerViewController *)playerCon{

    if (!_playerCon) {

        _playerCon=[[AVPlayerViewController alloc]init];

        _playerCon.player=[AVPlayer playerWithURL:_url];

//设置显示位置

//        _playerCon.view.frame=CGRectMake(100, 100, 250, 250);

//        [self.view addSubview:_playerCon.view];

        [self presentViewController:_playerCon animated:YES completion:nil];

    }

    return _playerCon;

}

- (void)viewDidLoad {

    [super viewDidLoad];

}

- (IBAction)caijiAction:(UIButton*)sender {

   if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

       [self presentViewController:self.imgPicker animated:YES completion:nil];

   }

}

- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    NSString *type =info[UIImagePickerControllerMediaType];

    if ([type isEqualToString:(__bridge NSString*)kUTTypeMovie]) {

        _url=info[UIImagePickerControllerMediaURL];

    }

    [self dismissViewControllerAnimated:YES completion:nil];

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker{

    [self dismissViewControllerAnimated:YES completion:nil];

}

- (IBAction)playAction:(UIButton*)sender {

  [self.playerCon player];

}

你可能感兴趣的:(iOS开发-简单的视频录制和播放)