AVPlayer

AVPlayer

  • 初始化AVPlayer

    + (instancetype)playerWithURL:(NSURL *)URL;
    
    + (instancetype)playerWithPlayerItem:(AVPlayerItem *)item;
    
    - (instancetype)initWithURL:(NSURL *)URL;
    
    - (instancetype)initWithPlayerItem:(AVPlayerItem *)item;
  • 媒体资源管理对象AVPlayerItem,每个AVPlayerItem对应一个视频资源

    NSString *urlStr=[NSString stringWithFormat:@"http://192.168.1.161/%i.mp4",videoIndex];
    urlStr =[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url=[NSURL URLWithString:urlStr];
    AVPlayerItem *playerItem=[AVPlayerItem playerItemWithURL:url];
  • 使用AVPlayerLayer展示AVPlayer:控制AVPlayerLayer来完成全屏等操作

        AVPlayerLayer *playerLayer=[AVPlayerLayer playerLayerWithPlayer:self.player];
    playerLayer.frame=self.container.frame;
    //playerLayer.videoGravity=AVLayerVideoGravityResizeAspect;//视频填充模式
    [self.container.layer addSublayer:playerLayer];
  • 播放暂停方法

    - (void)play;
    - (void)pause;
  • 通过属性rate了解播放状态

    如果rate为0说明是停止状态,1为正常状态 ;
    设置rate为0.0等同于pause方法,改为1.0等同于pause方法
  • 通过属性status了解播放状态

    AVPlayerStatusUnknown,
    AVPlayerStatusReadyToPlay,
    AVPlayerStatusFailed        
  • 获得播放进度方法addPeriodicTimeObserverForInterval

    [self.player addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
    float current=CMTimeGetSeconds(time);
    float total=CMTimeGetSeconds([playerItem duration]);
    NSLog(@"当前已经播放%.2fs.",current);
    NSLog(@" 播放进度%.2fs.",current/total);
  • 切换视频方法replaceCurrentItemWithPlayerItem

     还可以使用AVQueuePlayer处理播放列表切换

AVPlayerItem

  • 为AVPlayerItem添加视频播放完成通知AVPlayerItemDidPlayToEndTimeNotification

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.player.currentItem];
  • 通过KVO监控AVPlayerItem的statusloadedTimeRanges属性来获得 视频加载情况、缓冲情况、播放情况

    -(void)addObserverToPlayerItem:(AVPlayerItem *)playerItem{
        //监控状态属性,注意AVPlayer也有一个status属性,通过监控它的status也可以获得播放状态
        [playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
        //监控网络加载情况属性
        [playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
    }
    
    -(void)removeObserverFromPlayerItem:(AVPlayerItem *)playerItem{
        [playerItem removeObserver:self forKeyPath:@"status"];
        [playerItem removeObserver:self forKeyPath:@"loadedTimeRanges"];
    }
    
    -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
        AVPlayerItem *playerItem=object;
        if ([keyPath isEqualToString:@"status"]) {
            AVPlayerStatus status= [[change objectForKey:@"new"] intValue];
            if(status==AVPlayerStatusReadyToPlay){
            NSLog(@"正在播放...,视频总长度:%.2f",CMTimeGetSeconds(playerItem.duration));
        }
        }else if([keyPath isEqualToString:@"loadedTimeRanges"]){
            NSArray *array=playerItem.loadedTimeRanges;
            CMTimeRange timeRange = [array.firstObject CMTimeRangeValue];//本次缓冲时间范围
            float startSeconds = CMTimeGetSeconds(timeRange.start);
            float durationSeconds = CMTimeGetSeconds(timeRange.duration);
            NSTimeInterval totalBuffer = startSeconds + durationSeconds;//缓冲总长度
            NSLog(@"共缓冲:%.2f",totalBuffer);
        }
    }

AVQueuePlayer

是AVPlayer的子类,有一个items属性

AVPlayerLayer

你可能感兴趣的:(AVPlayer)