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 }
这便可以跳转进入的时候横屏了