iOS视频播放(二)整理

我的Demo地址--------->  点击打开链接

#pragma mark -  添加进度观察 - addProgressObserver

- (void)addProgressObserver {

    //  设置每秒执行一次

    [_playerHelper.getAVPlayer addPeriodicTimeObserverForInterval:CMTimeMake(11queueNULL usingBlock:^(CMTime time) {

        NSLog(@"进度观察 + %f"_topProgressSlider.value);

        //  获取当前时间

        CMTime currentTime = _playerHelper.getAVPlayer.currentItem.currentTime;

        //  转化成秒数

        CGFloat currentPlayTime = (CGFloat)currentTime.value / currentTime.timescale;

        //  总时间

        CMTime totalTime = playItem.duration;

        //  转化成秒

        _totalMovieDuration = (CGFloat)totalTime.value / totalTime.timescale;

        //  相减后

        _topProgressSlider.value = CMTimeGetSeconds(currentTime) / _totalMovieDuration;

        progressSlider = CMTimeGetSeconds(currentTime) / _totalMovieDuration;

        NSLog(@"%f"_topProgressSlider.value);

        NSDate *pastDate = [NSDate dateWithTimeIntervalSince1970: currentPlayTime];

        _topPastTimeLabel.text = [self getTimeByDate:pastDate byProgress: currentPlayTime];

        CGFloat remainderTime = _totalMovieDuration - currentPlayTime;

        NSDate *remainderDate = [NSDate dateWithTimeIntervalSince1970: remainderTime];

        _topRemainderLabel.text = [self getTimeByDate:remainderDate byProgress: remainderTime];

        if (_isFirstRotatorTap) {

            [self setTopRightBottomViewShowToHidden];

        } else {

            [self setTopRightBottomViewHiddenToShow];

        }

    }];

    //  设置topProgressSlider图片

    UIImage *thumbImage = [UIImage imageNamed:@"slider-metal-handle.png"];

    [self.topProgressSlider setThumbImage:thumbImage forState:UIControlStateHighlighted];

    [self.topProgressSlider setThumbImage:thumbImage forState:UIControlStateNormal];

}


- (void)addAVPlayer{

    playItem = [AVPlayerItem playerItemWithURLself.movieURL];

    self.playerHelper = [[ChuLjMovieHelper allocinit];

    [_playerHelper initAVPlayerWithAVPlayerItem:playItem];

    //  创建显示层

    self.playerLayer = [AVPlayerLayer playerLayerWithPlayer_playerHelper.getAVPlayer];

    _playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;

    //  竖屏的时候frame

    [self setVerticalFrame];

    //  这是视频的填充模式默认为AVLayerVideoGravityResizeAspect

    _playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

    //  插入到view层上面没有用addSubLayer

    [self.view.layer insertSublayer:_playerLayer atIndex:0];

    //  添加进度观察

    [self addProgressObserver];

    [self addObserverToPlayerItemplayItem];

}


//  观察者的方法会在加载好后触发可以在这个方法中保存总文件的大小用于后面的进度的实现

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{

    AVPlayerItem *playerItem = (AVPlayerItem *)object;

    if ([keyPath isEqualToString:@"status"]) {

        AVPlayerStatus status = [[change objectForKey:@"new"intValue];

        if (status == AVPlayerStatusReadyToPlay) {

            NSLog(@"正在播放...,视频总长度: %.2f",CMTimeGetSeconds(playerItem.duration));

            CMTime totalTime = playerItem.duration;

            self.totalMovieDuration = (CGFloat)totalTime.value / totalTime.timescale;

        }

    }

    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);

        NSLog(@"进度 + %f"progressSlider);

        self.topProgressSlider.value = progressSlider;

    }

}

相关知识,demo有注释,不明白的地方欢迎留言。



你可能感兴趣的:(ios,视频,视频播放)