由于MPMoviePlayerController與MPMoviePlayerViewController在iOS9.0之后被棄用,所以采用AVPlayer以及AVPlayerViewController來實(shí)現(xiàn)基礎(chǔ)的視頻播放。
AVPlayerViewController
這個(gè)是蘋果給我們封裝好的了、使用也非常簡(jiǎn)單、但是界面貌似沒發(fā)自定義、適合沒有界面要求的需求。但是簡(jiǎn)單??!簡(jiǎn)單??!
廢話不說、直接上代碼了解一下:
? ? //步驟1:獲取視頻路徑
? ? NSString *webVideoPath = @"https://vd2.bdstatic.com/mda-kidkfudrpqgg8891/sc/cae_h264_clips/mda-kidkfudrpqgg8891.mp4";
? ? NSURL*webVideoUrl = [NSURLURLWithString:webVideoPath];
? ? //步驟2:創(chuàng)建AVPlayer
? ? AVPlayer * avPlayer = [[AVPlayer alloc]initWithURL:webVideoUrl];
? ? //步驟3:使用AVPlayer創(chuàng)建AVPlayerViewController,并跳轉(zhuǎn)播放界面
? ? AVPlayerViewController *avPlayerVC =[[AVPlayerViewController alloc] init];
? ? avPlayerVC.player= avPlayer;
? ? [self presentViewController:avPlayerVC animated:YES completion:nil];? ?
沒錯(cuò)、已經(jīng)結(jié)束了!開心的用起來吧!??
AVPlayer
對(duì)于AVPlayer來說就相對(duì)麻煩一點(diǎn)了、相對(duì)誰?相對(duì)AVPlayerViewController??!可見也不怎么麻煩對(duì)吧??。
首先新建一個(gè)普普通通的viewController,導(dǎo)入頭文件、創(chuàng)建對(duì)象
#import??<AVFoundation/AVFoundation.h>
@property (nonatomic ,strong) AVPlayer * avplayer;
@property (nonatomic ,strong) AVPlayerItem * avplayerItem;
@property (nonatomic ,strong) AVPlayerLayer * playLayer;
初始化相關(guān)對(duì)象和數(shù)據(jù)設(shè)置
? ?//本地視頻
? ? //? ? NSString * localPath = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"mp4"];
? ? //? ? NSURL * localUrl = [NSURL fileURLWithPath:localPath];
? ? //網(wǎng)絡(luò)視頻
? ? NSString * webPath = @"https://vd2.bdstatic.com/mda-kidkfudrpqgg8891/sc/cae_h264_clips/mda-kidkfudrpqgg8891.mp4";
? ? NSURL* webUrl = [NSURLURLWithString:webPath];
? ? //初始化播放器
? ? self.avplayerItem = [[AVPlayerItem alloc] initWithURL:webUrl];
? ? self.avplayer = [[AVPlayer alloc] initWithPlayerItem:self.avplayerItem];
? ? //創(chuàng)建顯示視頻的AVPLayerLayer
? ? /*
?? ? AVLayerVideoGravityResizeAspectFill等比例鋪滿,寬或高有可能出屏幕
?? ? AVLayerVideoGravityResizeAspect 等比例? 默認(rèn)
?? ? AVLayerVideoGravityResize 完全適應(yīng)寬高
?? ? */
?? ? _playLayer = [AVPlayerLayer playerLayerWithPlayer:self.avplayer];
? ? _playLayer.videoGravity = AVLayerVideoGravityResizeAspect;
? ? _playLayer.frame = CGRectMake(0, 100, DEVICEWIDTH, DEVICEHEIGHT/2);
? ? [self.view.layer addSublayer:_playLayer];
? ?//開始播放
? ? [self.avplayer play];
這樣一個(gè)簡(jiǎn)單的視頻播放就完成了!但是、聽好了但是,這才完成了3分之一而已、剩下的該做什么呢?
暫停?
關(guān)于視頻的暫停播放、很簡(jiǎn)單
? ? [self.avplayerpause];
? ? 結(jié)束了!
繼續(xù)播放?
那就
?[self.avplayer play];
?what?就這樣?
對(duì)啊、就是這么簡(jiǎn)單??!
播放監(jiān)聽?
視頻播放肯定有自己的狀態(tài)、是否緩沖完畢、加載失敗、異常、視頻長度、播放進(jìn)度等情況。那么該如何監(jiān)聽處理呢?
利用KVO給AVPlayer的currentItem添加屬性監(jiān)聽、監(jiān)聽“status”和“l(fā)oadedTimeRanges”關(guān)鍵字屬性。
? ? [self.avplayer.currentItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
調(diào)用屬性監(jiān)聽方法
//屬性監(jiān)聽
- (void)observeValueForKeyPath:(NSString *)keyPath
? ? ? ? ? ? ? ? ? ? ? ofObject:(id)object
? ? ? ? ? ? ? ? ? ? ? ? change:(NSDictionary *)change
?? ? ? ? ? ? ? ? ? ? ? context:(void*)context
{
? ? AVPlayerItem* playitem = (AVPlayerItem*)object;
? ? if([keyPathisEqualToString:@"status"]) {
? ? ? ? AVPlayerStatus status = [[change objectForKey:@"new"] intValue];
? ? ? ? switch(status) {
? ? ? ? ? ? case AVPlayerStatusReadyToPlay:{ //準(zhǔn)備就緒
? ? ? ? ? ? ? ? //獲取視頻時(shí)間
? ? ? ? ? ? ? ? CMTimeduration? = playitem.duration;
? ? ? ? ? ? ? ? NSString* time = [selfformatTimeWithTimeInterVal:CMTimeGetSeconds(duration)];
? ? ? ? ? ? ? ? //顯示視頻總長度? ?自定義一個(gè)label就可以
? ? ? ? ? ? ? ? self.timeLabel.text= time;
? ? ? ? ? ? ? ? //開啟滑塊 為了控制播放進(jìn)度
? ? ? ? ? ? ? ? self.sliderView.enabled=YES;
? ? ? ? ? ? ? ? [self.avplayer? ?play];
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? case AVPlayerStatusFailed:{ //失敗、可以嘗試重試
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? case AVPlayerStatusUnknown:{ //異常
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? default:
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? }
}
通過上邊的方法就可以獲取到視頻的狀態(tài)和長度、那么接下來就要對(duì)播放過程進(jìn)行處理。
__weak typeof(self) weakSelf = self;
? ? self.timeObserver = [self.avplayer addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
? ? ? ? //當(dāng)前播放時(shí)間
? ? ? ? NSTimeInterval nowTime = CMTimeGetSeconds(time);
? ? ? ? //總時(shí)間
? ? ? ? NSTimeInterval totaltime = CMTimeGetSeconds(weakSelf.avplayer.currentItem.duration);
? ? ? ? //滑塊進(jìn)度
? ? ? ? weakSelf.sliderView.value= nowTime/totaltime;
? ? ? ? //倒計(jì)時(shí)
? ? ? ? weakSelf.nowtimeLabel.text= [weakSelfformatTimeWithTimeInterVal:nowTime];
? ? }];
上邊這一段代碼就可以動(dòng)態(tài)的顯示播放的時(shí)間、和播放的進(jìn)度,具體顯示的樣式以個(gè)人實(shí)際情況而定!
好了、先到這、后期有需求接著完善!