avplayer 播放視頻
首先介紹幾個方法吧和屬性吧。
- (id)addPeriodicTimeObserverForInterval:(CMTime)interval queue:(dispatch_queue_t)queue usingBlock:(void?(^)(CMTime?time))block
這個方法可以用于跟新進度條。
- (void)seekToTime:(CMTime)time completionHandler:(void?(^)(BOOL?finished))completionHandler
這個是設置從哪個位置開始播放
?CMTime?changedTime =?CMTimeMakeWithSeconds(timeFloat,?1.0);
獲取?CMTime
rate 播放的狀態 ?0 表示暫停 ?1表示播放
volume 聲音
下面是主要的代碼
1 +(Class)layerClass 2 { 3 return [AVPlayerLayer class]; 4 } 5 6 -(void)setMoviePlayer:(AVPlayer *)moviePlayer 7 { 8 AVPlayerLayer *layer = (AVPlayerLayer *)[self layer]; 9 layer.videoGravity = AVLayerVideoGravityResizeAspectFill; 10 layer.player = moviePlayer; 11 } 12 13 -(AVPlayer *)moviePlayer 14 { 15 AVPlayerLayer *layer = (AVPlayerLayer *)[self layer]; 16 return layer.player; 17 }
?
?
?
1 /** 2 * 初始化視頻播放器 3 * 4 * @param movieUrl url網址 5 */ 6 -(void)setMovieUrl:(NSString *)movieUrl 7 { 8 _movieUrl = movieUrl; 9 _movieItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:movieUrl]]; 10 _moviePlayer = [AVPlayer playerWithPlayerItem:_movieItem]; 11 _moviePlayer.volume = 0.5; 12 [_voiceView setProgressView:0.5]; 13 _playerLayer.moviePlayer = _moviePlayer; 14 [self addNotifiction]; 15 [_playerLayer.moviePlayer pause]; 16 }
/*** 添加監聽*/ -(void)addNotifiction {[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinishedNotifiction:) name:AVPlayerItemDidPlayToEndTimeNotification object:_playerLayer.moviePlayer.currentItem];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackError:) name:AVPlayerItemNewAccessLogEntryNotification object:_playerLayer.moviePlayer.currentItem];[_movieItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil]; }
?
?
1 //獲取播放的進度 2 AVPlayerItem *mobieItem = _movieItem; 3 __block LSCacheView * progress = _progressView; 4 [_moviePlayer addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) { 5 float current=CMTimeGetSeconds(time); 6 float total=CMTimeGetSeconds([mobieItem duration]); 7 if (current) { 8 [progress setProgressView:(current/total)]; 9 } 10 }];
?
if ([keyPath isEqualToString:@"loadedTimeRanges"]) {NSTimeInterval timeInterval = [self availableDuration];NSTimeInterval cuttTime = CMTimeGetSeconds(_movieItem.currentTime);CMTime duration = _movieItem.duration;CGFloat totalDuration = CMTimeGetSeconds(duration);if (cuttTime < timeInterval){//有時候網絡卡會自動暫停,通過這個方式可以避免這種方式if (_playerLayer.moviePlayer.rate == 0 && _playerButton.isSelected == YES){[_playerLayer.moviePlayer play];}}else{}}
?
?以上這些我認為就是主要的代碼了。哦 ? 還有個進入全屏播放的方式,大部分的項目都用到了UINavigationController,所以我用的橫屏播放方式是模態跳轉的方式
在需要橫屏的controller添加以下代碼
1 - (BOOL)shouldAutorotate 2 { 3 return NO; 4 } 5 6 -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 7 { 8 return UIInterfaceOrientationLandscapeRight; 9 } 10 11 -(NSUInteger)supportedInterfaceOrientations 12 { 13 return UIInterfaceOrientationMaskLandscapeRight; 14 }
?
這便可以跳轉進入的時候橫屏了