两种视频播放形式和 视频截图

#import "ViewController.h"

#import <MediaPlayer/MediaPlayer.h>

#import <AVFoundation/AVFoundation.h>


@interface ViewController ()

@property(nonatomic,weak)UIImageView  *image;

@property(nonatomic,strong)MPMoviePlayerController*controll;

@property(nonatomic,weak) UIView *avView;

@property(nonatomic,strong)AVAssetImageGenerator*gener;

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    UIButton *btn=[[UIButton alloc]init];

    [btn setTitle:@"model出视频控制器" forState:UIControlStateNormal];

    btn.backgroundColor=[UIColor grayColor];

    btn.frame=CGRectMake(10, 50, 150, 50);

    [self.view addSubview:btn];

    [btn addTarget:self action:@selector(btnModelController) forControlEvents:UIControlEventTouchUpInside];

    

    

    UIButton *btn1=[[UIButton alloc]init];

    [btn1 setTitle:@"视图上显示视频" forState:UIControlStateNormal];

    btn1.backgroundColor=[UIColor grayColor];

    btn1.frame=CGRectMake(200, 50, 150, 50);

    [self.view addSubview:btn1];

    [btn1 addTarget:self action:@selector(btnShowInCurrentController) forControlEvents:UIControlEventTouchUpInside];

    

    UIButton *btn2=[[UIButton alloc]init];

    [btn2 setTitle:@"点击截图" forState:UIControlStateNormal];

    btn2.backgroundColor=[UIColor grayColor];

    btn2.frame=CGRectMake(self.view.bounds.size.width/2+20, 110, 100,100);

    [self.view addSubview:btn2];

    [btn2 addTarget:self action:@selector(cutImage) forControlEvents:UIControlEventTouchUpInside];


    

    UIImageView *image=[[UIImageView alloc]init];

    image.frame=CGRectMake(0, 110, self.view.bounds.size.width/2, self.view.bounds.size.width/2);

    [self.view addSubview:image];

    image.backgroundColor=[UIColor grayColor];

    self.image=image;

    

    UIView *avView=[[UIView alloc]init];

    avView.frame=CGRectMake(0, 110+ self.view.bounds.size.width/2,self.view.bounds.size.width ,self.view.bounds.size.height-110-self.view.bounds.size.width/2);

    avView.backgroundColor=[UIColor greenColor];

    [self.view addSubview:avView];

    self.avView=avView;

    

    }

//model出来的控制器

-(void)btnModelController

{

    NSURL *url=[NSURL fileURLWithPath: [[NSBundle mainBundle]pathForResource:@"Alizee_La_Isla_Bonita.mp4" ofType:nil]];

    MPMoviePlayerViewController *vc=[[MPMoviePlayerViewController alloc]initWithContentURL:url];

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

}


-(void)btnShowInCurrentController

{

    NSURL *url=[NSURL fileURLWithPath: [[NSBundle mainBundle]pathForResource:@"Alizee_La_Isla_Bonita.mp4" ofType:nil]];

    self.controll=[[MPMoviePlayerController alloc]initWithContentURL:url];

    [self.avView addSubview:self.controll.view];

    self.controll.view.frame=self.avView.bounds;

    //播放

    [self.controll play];

}

//截图视频中某一时刻的图片

-(void)cutImage

{

    NSURL *url=[NSURL fileURLWithPath: [[NSBundle mainBundle]pathForResource:@"Alizee_La_Isla_Bonita.mp4" ofType:nil]];

    //获取资源

    AVAsset *asset=[AVAsset assetWithURL:url];

    //建立图片生成器

    AVAssetImageGenerator*gener=[[AVAssetImageGenerator alloc]initWithAsset:asset];

    self.gener=gener;

    //获取此刻播放进度

     NSTimeInterval time= [self.controll currentPlaybackTime];

    CMTime cTime=CMTimeMake(time, 1.0);

    NSValue *value=[NSValue valueWithCMTime:cTime];

    //截图

    [gener generateCGImagesAsynchronouslyForTimes:@[value] completionHandler:^(CMTime requestedTime, CGImageRef images, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error) {

        //添加到主队列有问题,主线程没问题

        dispatch_async(dispatch_get_main_queue(), ^{

            UIImage *needImage=[UIImage imageWithCGImage:images];

            [self.image setImage:needImage];

        });

    

    }];

}






- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

}


@end

原文地址:https://www.cnblogs.com/tangranyang/p/4665460.html