#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