1. 程式人生 > >AVPlayer的使用,帶緩沖

AVPlayer的使用,帶緩沖

interface elf tor track nds ray imp idl 一個


#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
{
AVPlayer *player;
AVPlayerItem *playerItem;

UIProgressView * progressView;
UISlider *_slider;

//推斷slider是否按下,
BOOL isOpen;
}
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
[self createUI];
//進行初始化創建
NSURL *url = [NSURL fileURLWithPath:@"/Users/qianfeng01/Downloads/千鋒Swift視頻教程-1.Swift語言介紹.mp4"];
playerItem = [[AVPlayerItem alloc]initWithURL:url];
//創建player
player = [[AVPlayer alloc]initWithPlayerItem:playerItem];
//生成layer層
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:player];
//設置坐標
layer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

//把layer層假如到self.view.layer中
[self.view.layer addSublayer:layer];
//進行播放
[player play];

/**以上是主要的播放界面。可是沒有前進後退**/
//觀察是否播放,KVO進行觀察,觀察playerItem.status
[playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];

//觀察緩存如今的進度,KVO進行觀察,觀察loadedTimeRanges
[playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
}

//觀察是否播放
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
if ([keyPath isEqualToString:@"status"]) {
if (playerItem.status == AVPlayerStatusReadyToPlay) {
NSLog(@"開始播放");
//須要開始獲取數據,包含播放的總時長。播放的緩存,播放的當時時間
[self loadData];
}else{
NSLog(@"播放失敗");
}
}else{
//kvo觸發的另外一個屬性
NSArray *array = [playerItem loadedTimeRanges];
//獲取範圍i
CMTimeRange range = [array.firstObject CMTimeRangeValue];
//從哪兒開始的
CGFloat start = CMTimeGetSeconds(range.start);
//緩存了多少
CGFloat duration = CMTimeGetSeconds(range.duration);
//一共緩存了多少
CGFloat allCache = start+duration;
NSLog(@"緩存了多少數據:%f",allCache);

//設置緩存的百分比
CMTime allTime = [playerItem duration];
//轉換
CGFloat time = CMTimeGetSeconds(allTime);
CGFloat y = allCache/time;
NSLog(@"緩存百分比:--------%f",y);
progressView.progress = y;
}
}


#pragma mark -- 獲取播放數據
- (void)loadData{

__weak AVPlayerItem *xx = playerItem;
__weak UISlider *cc = _slider;
//第一個參數是每隔多長時間調用一次。在這裏設置的是每隔1秒調用一次
[player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:NULL usingBlock:^(CMTime time) {
//當前播放時間
CGFloat current = xx.currentTime.value/xx.currentTime.timescale;

//獲取總時長
CMTime time1 = xx.duration;
float x = CMTimeGetSeconds(time1);
NSLog(@"當前播放的秒數------- %f --------%f",current,x);

//設置滑動條進度
float v = current/x;

//推斷slider是否按下,按下去就先別賦值
if (!isOpen) {
cc.value = v;
}

}];

}

#pragma mark --- 創建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];


//加入點擊事件
[_slider addTarget:self action:@selector(sliderClick:) forControlEvents:UIControlEventTouchUpInside];
//擡起來的事件
[_slider addTarget:self action:@selector(sliderClickUp:) forControlEvents:UIControlEventTouchUpInside];
}

//加入點擊事件
- (void)sliderClick:(UISlider *)slider{
NSLog(@"加入點擊事件");
isOpen = YES;
}

//擡起來的事件
- (void)sliderClickUp:(UISlider *)slider{
NSLog(@"擡起來的事件");
isOpen = NO;

//從這裏開始播放
CGFloat g = slider.value;
//獲取總時長
CMTime time1 = playerItem.duration;
float x = CMTimeGetSeconds(time1);
//進行播放
[player seekToTime:CMTimeMake(x * g,1)];
//播放
[player play];

}


@end

AVPlayer的使用,帶緩沖