Two video playback formats and video screenshots

Source: Internet
Author: User

#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 out video controller" 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];

The video "Forstate:uicontrolstatenormal" is displayed on the [btn1 settitle:@] view;

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:@ "click" 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, 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 out of the controller

-(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;

Play

[Self.controll play];

}

A picture of a moment in a video

-(void) cutimage

{

Nsurl *url=[nsurl Fileurlwithpath: [[NSBundle mainbundle]pathforresource:@ "Alizee_la_isla_bonita.mp4" OfType:nil]];

Get Resources

Avasset *asset=[avasset Assetwithurl:url];

Create a picture generator

Avassetimagegenerator*gener=[[avassetimagegenerator Alloc]initwithasset:asset];

Self.gener=gener;

Get the playback progress at this moment

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) {

Add to the Home column there is a problem, the main thread no problem

Dispatch_async (Dispatch_get_main_queue (), ^{

UIImage *needimage=[uiimage Imagewithcgimage:images];

[Self.image Setimage:needimage];

});

}];

}






-(void) didreceivememorywarning {

[Super didreceivememorywarning];

}


@end

Two video playback formats and videos

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.