AVPlayer实现简单播放视频

播放视频以前我们可以使用MPMoviePlayerController,虽然MP很简单,但是不能定制UI,并且很多功能不能实现,AVFoundation中的AVPlayer应运而生,首先我们来看一幅图:

AVPlayer实现简单播放视频_第1张图片
AVPlayer.png

实现一个简单的网络视频播放器,需要注意三个重要对象:
(1)AVPlayer:负责播放视频
(2)AVPlayerItem:负责管理视频数据,apple给出的API解释:A player item manages the presentation state of an asset with which it is associated. A player item contains player item tracks—instances of [AVPlayerItemTrack](https://developer.apple.com/library/mac/documentation/AVFoundation/Reference/AVPlayerItemTrack_Class/Reference/Reference.html#//apple_ref/occ/cl/AVPlayerItemTrack)—that correspond to the tracks in the asset
(3) AVPlayerLayer:视频播放界面显示图层
废话不多说,直接上代码:

static void *PlayViewStatusObservationContext = &PlayViewStatusObservationContext;

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [_player pause];
    _player = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:@"http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4"]];

//播放状态
    [item addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:PlayViewStatusObservationContext];
    //缓冲总时间
    [item addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:PlayViewStatusObservationContext];
    // 缓冲区空了,需要等待数据
    [item addObserver:self forKeyPath:@"playbackBufferEmpty" options: NSKeyValueObservingOptionNew context:PlayViewStatusObservationContext];
    // 缓冲区有足够数据可以播放了
    [item addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options: NSKeyValueObservingOptionNew context:PlayViewStatusObservationContext];

    _player = [[AVPlayer alloc] initWithPlayerItem:item];
    AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:_player];
    layer.frame = self.view.bounds;
    layer.contentsScale = [UIScreen mainScreen].scale;
    layer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.view.layer addSublayer:layer];

    _currentTime = [[UILabel alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 20, 120, 20)];
    _currentTime.textAlignment = 1;
    _currentTime.textColor = [UIColor whiteColor];
    _currentTime.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_currentTime];
    
    _slider = [[UISlider alloc] initWithFrame:CGRectMake(120, [UIScreen mainScreen].bounds.size.height - 20, [UIScreen mainScreen].bounds.size.width - 240, 5.0)];
    [self.view addSubview:_slider];

    
    _totalTime = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width  - 120, _slider.frame.origin.y, 120, 20)];
    _totalTime.textAlignment = 1;
    _totalTime.textColor = [UIColor whiteColor];
    _totalTime.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_totalTime];
    
    _totalTime.text = [self toTimeStrWithSeconds:CMTimeGetSeconds(_player.currentItem.duration)];

    [_slider addTarget:self action:@selector(slideAction:) forControlEvents:UIControlEventValueChanged];
    //播放器添加定时器,CMTimtMake(a,b)相当于a/b秒就会进入这个block
    [_player  addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0)
                                           queue:dispatch_get_main_queue()
                                      usingBlock:^(CMTime time) {
                                          float currentValue = CMTimeGetSeconds(time);
                                          float totalValue = CMTimeGetSeconds(item.duration);
                                          _totalTime.text = [self toTimeStrWithSeconds:totalValue];
                                          _currentTime.text = [self toTimeStrWithSeconds:currentValue];
                                          [_slider setValue: currentValue / totalValue];
                                      }];
    
    //播放进度相关
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playtoEndAction:) name:AVPlayerItemDidPlayToEndTimeNotification object:_player];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(jumpedTimeAction:) name:AVPlayerItemTimeJumpedNotification object:_player];
    
    //播放后台相关
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(applicationDidBackground:)
                                                 name:UIApplicationDidEnterBackgroundNotification object:_player];

注意,在这里添加KVO操作的是AVPayerItem对象而不是AVPlayer对象,注意在dealloc里面移除KVO,在KVO方法里面可以获取Item的一些数据,比如播放状态、缓冲数据大小等等,这对于我们定制播放界面UI很有帮助,像开发者可以自己定制进度条,获取播放进度、获取缓冲进度等等:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([object isKindOfClass:[AVPlayerItem class]]) {
        AVPlayerItem *item =(AVPlayerItem *)object;
        if ([keyPath rangeOfString:@"status"].length) {
            //播放状态
            if (item.status == AVPlayerStatusReadyToPlay) {
                [_player play];
            } else if (item.status == AVPlayerStatusFailed) {
                NSLog(@"失败!");
            } else if (item.status == AVPlayerStatusUnknown) {
                NSLog(@"未知!");
            }
        } else if ([keyPath rangeOfString:@"loadedTimeRanges"].length) {
            //缓冲
            NSArray *caches = item.loadedTimeRanges;
            CMTimeRange range = [caches.firstObject CMTimeRangeValue];
            float startSeconds = CMTimeGetSeconds(range.start);
            float durationSeconds  = CMTimeGetSeconds(range.duration);
            float cachesSeconds =  startSeconds + durationSeconds;
            NSString *subStr = @"%";
            float totalDuration = CMTimeGetSeconds(item.duration);
            NSLog(@"共缓冲了%@%.2f",subStr,cachesSeconds / totalDuration * 100.0);
            
        }
    }
}

你可能感兴趣的:(AVPlayer实现简单播放视频)