视频播放学习

2020-04-19  本文已影响0人  冷武橘

一、 AVPlayer

   //1、获取url
    NSURL *url=[[NSBundle mainBundle]URLForResource:@"EP00.mp4" withExtension:nil];
    
    //2、创建AVPlayerItem (AVPlayerItem:管理资源对象,提供播放数据源)
    AVPlayerItem *playItem=[AVPlayerItem playerItemWithURL:url];
    
    //3、创建 AVPlayer(控制播放器的播放,暂停,播放速度)
    AVPlayer *player=[AVPlayer playerWithPlayerItem:playItem];
    
    //4、添加AVPlayerLayer(AVPlayerLayer:负责显示视频,如果没有添加该类,只有声音没有画面)
    AVPlayerLayer *playlayer = [AVPlayerLayer playerLayerWithPlayer:player];
    [self.view.layer addSublayer:playlayer];
    playlayer.frame=CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width * 9 / 16);
    
    //5、开始播放
    [player play];
    //5、开始播放
    [player play];

1、AVPlayer一次只能播放一个媒体资源。播放器可以使用其- (void)replaceCurrentItemWithPlayerItem:(nullable AVPlayerItem *)item方法来切换视频
2、播放器的播放状态判断可以通过播放器的播放速度 rate 来获得,如果 rate 为0说明是停止状态,为1时则是正常播放状态。想要获取视频播放情况、缓冲情况等的实时变化,可以通过 KVO 监控 AVPlayerItem 的 status、loadedTimeRanges 等属性来获得。

//使用KVO监测playItem.status,可以获取播放状态的变化
[self.playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];

 -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
     if ([keyPath isEqualToString:@"status"]) {
        switch (_playerItem.status) {
       case AVPlayerItemStatusReadyToPlay://准备播放
     //推荐将视频播放放在这里
       [self play];
        break;
        case AVPlayerItemStatusUnknown://未知资源
        NSLog(@"AVPlayerItemStatusUnknown");
        break;

   case AVPlayerItemStatusFailed://加载失败
         NSLog(@"AVPlayerItemStatusFailed")
        break;
       default:
       break;
   }

}

2.2 、播放进度的控制


#pragma mark - 开始拖拽进度
- (void)valuebeginchangewithslider:(UISlider *)slider{
     [self removetimer];
}

#pragma mark - 正在拖拽中
- (void)valuechangewithslider:(UISlider *)slider{
    NSTimeInterval duration = CMTimeGetSeconds(self.player.currentItem.duration);
    NSTimeInterval currenttime = slider.value*duration;
    self.toolView.timeLable.text=[self stringWithCurrentTime:currenttime duration:duration];
   
}

#pragma mark- 结束拖拽
- (void)valueendchangewithslider:(UISlider *)slider{
    //添加定时器
    [self addtimer];
    
    //获取当前已播放时长
    NSTimeInterval currentTime = CMTimeGetSeconds(self.player.currentItem.duration) * slider.value;
    
    //跳跃到当前拖拽到的时间点
     [self.player seekToTime:CMTimeMakeWithSeconds(currentTime, NSEC_PER_SEC) toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero];
    
    //继续开始播放视频
    [self.player play];
 
    
}

#pragma mark - 添加定时器
- (void)addtimer{
    NSTimer *progresstimer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateUI) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:progresstimer forMode:NSRunLoopCommonModes];
    self.timer=progresstimer;
}

#pragma mark - 移除定时器
- (void)removetimer{
    [self.timer invalidate];
    self.timer=nil;
    
}


#pragma mark - 时长格式处理
- (NSString *)stringWithCurrentTime:(NSTimeInterval)currentTime duration:(NSTimeInterval)duration
{
    NSInteger dMin = duration / 60;
    NSInteger dSec = (NSInteger)duration % 60;
    
    NSInteger cMin = currentTime / 60;
    NSInteger cSec = (NSInteger)currentTime % 60;
    
    NSString *durationString = [NSString stringWithFormat:@"%02ld:%02ld", dMin, dSec];
    NSString *currentString = [NSString stringWithFormat:@"%02ld:%02ld", cMin, cSec];
    
    return [NSString stringWithFormat:@"%@/%@", currentString, durationString];
}

#pragma mark - 更新当前的时间显示和进度位置
- (void)updateUI{
    NSTimeInterval duration = CMTimeGetSeconds(self.player.currentItem.duration);
    NSTimeInterval currentTime = CMTimeGetSeconds(self.player.currentTime);
    NSString *timestring=[self stringWithCurrentTime:currentTime duration:duration];
    self.toolView.timeLable.text=timestring;
    self.toolView.slider.value=currentTime/duration;
    
}

2.3、视频全屏播放
思路:新建一个全屏控制器让这个控制器只支持向左的水平方向,然后点击全屏按钮.将播放画面添加到全屏view上

@implementation FullViewController

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    
    return  UIInterfaceOrientationMaskLandscapeLeft;//只支持向左旋转的方向
}

    if (isfull) {
    [self presentViewController:self.fullVc animated:YES completion:^{
        
            //添加到全屏控制器上
            [self.fullVc.view addSubview:self.playView];
            [self.playView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.equalTo(self.fullVc.view);
            }];
            
    }];
    }else{
        [self.fullVc dismissViewControllerAnimated:YES completion:^{
            //添加到控制器上
            [self.view addSubview:self.playView];
            //恢复的frame
            [playview mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.equalTo(self.view);
                make.bottom.equalTo(self.view);
            }];
        }];
    }
    
上一篇下一篇

猜你喜欢

热点阅读