#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface Viewcontroller ()
{
Avplayer *player;
Avplayeritem *playeritem;
Uiprogressview * Progressview;
UISlider *_slider;
Determine if the slider is pressed,
BOOL IsOpen;
}
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
[Self createui];
To initialize the creation of
Nsurl *url = [Nsurl fileurlwithpath:@ "/users/qianfeng01/downloads/Qian Feng Video tutorial -1.swift language introduction. mp4"];
Playeritem = [[Avplayeritem Alloc]initwithurl:url];
Create player
Player = [[Avplayer Alloc]initwithplayeritem:playeritem];
Creating layer Layers
Avplayerlayer *layer = [Avplayerlayer Playerlayerwithplayer:player];
Set coordinates
Layer.frame = CGRectMake (0, 0, self.view.frame.size.width, self.view.frame.size.height);
Put the layer in the Self.view.layer.
[Self.view.layer Addsublayer:layer];
To play
[Player play];
/** above is the basic playback interface, but there is no forward and backward **/
Observe whether or not to play, kvo to observe, observe playeritem.status
[Playeritem addobserver:self forkeypath:@ "status" Options:nskeyvalueobservingoptionnew Context:nil];
Observe the current progress of the cache, kvo to observe, observe loadedtimeranges
[Playeritem addobserver:self forkeypath:@ "loadedtimeranges" Options:nskeyvalueobservingoptionnew Context:nil];
}
Watch to see if it plays
-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: (nsdictionary *) Change context: (void *) context{
if ([KeyPath isequaltostring:@ "status"]) {
if (Playeritem.status = = Avplayerstatusreadytoplay) {
NSLog (@ "start playback");
Need to start getting data, including the total time played, the cache played, the time played
[Self loaddata];
}else{
NSLog (@ "playback failed");
}
}else{
Another attribute triggered by the KVO
Nsarray *array = [Playeritem loadedtimeranges];
Get Range I
Cmtimerange range = [Array.firstobject cmtimerangevalue];
Where did that start?
CGFloat start = Cmtimegetseconds (Range.Start);
How much was cached?
CGFloat duration = cmtimegetseconds (range.duration);
How much has been cached?
CGFloat allcache = start+duration;
NSLog (@ "How much data is cached:%f", Allcache);
Set Percentage of cache
Cmtime alltime = [Playeritem duration];
Transformation
CGFloat time = Cmtimegetseconds (alltime);
CGFloat y = allcache/time;
NSLog (@ "cache percentage:--------%f", y);
progressview.progress = y;
}
}
#pragma mark-Get playback data
-(void) loaddata{
__weak Avplayeritem *xx = Playeritem;
__weak UISlider *cc = _slider;
The first parameter is called every few times, and is set here every 1 seconds
[Player Addperiodictimeobserverforinterval:cmtimemake (1, 1) queue:null usingblock:^ (cmtime time) {
Current playback time
CGFloat current = Xx.currenttime.value/xx.currenttime.timescale;
Get the total length of time
Cmtime time1 = xx.duration;
float x = cmtimegetseconds (time1);
NSLog (@ "The number of seconds currently played-------%f--------%f", current,x);
Set the slide bar progress
Float v = current/x;
Determine if the slider is pressed, and don't assign a value until you press it down.
if (!isopen) {
Cc.value = v;
}
}];
}
#pragma mark---Create UI
-(void) createui{
Progressview = [[Uiprogressview Alloc]initwithprogressviewstyle:uiprogressviewstylebar];
Progressview.frame = CGRectMake (0, 460, self.view.frame.size.width, 20);
[Self.view Addsubview:progressview];
_slider = [[UISlider alloc]initwithframe:cgrectmake (0, 480, Self.view.frame.size.width, 20)];
[Self.view Addsubview:_slider];
Add a Click event
[_slider addtarget:self Action: @selector (Sliderclick:) forcontrolevents:uicontroleventtouchupinside];
Lifting up the event
[_slider addtarget:self Action: @selector (sliderclickup:) forcontrolevents:uicontroleventtouchupinside];
}
Add a Click event
-(void) Sliderclick: (UISlider *) slider{
NSLog (@ "Add click event");
IsOpen = YES;
}
Lifting up the event
-(void) Sliderclickup: (UISlider *) slider{
NSLog (@ "Lifted up events");
IsOpen = NO;
Start playing from here
CGFloat g = slider.value;
Get the total length of time
Cmtime time1 = playeritem.duration;
float x = cmtimegetseconds (time1);
To play
[Player Seektotime:cmtimemake (X * g,1)];
Play
[Player play];
}
@end
Use of Avplayer, with cushioning