AVPlayer的一些基本用法,缓存监听等

</pre><pre name="code" class="objc"><pre name="code" class="objc"><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo; color: rgb(201, 27, 19);"><span style="font-size:10px;"><span style="font-variant-ligatures: no-common-ligatures; color: rgb(130, 46, 14);">#define URLSTR_LIUCHANG @</span><span style="font-variant-ligatures: no-common-ligatures;">"https://portalvhdslhjkp62sbd1zh.blob.core.chinacloudapi.cn/upload/9533522808.f4v.mp4"</span></span></p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo; color: rgb(201, 27, 19);"><span style="font-variant-ligatures: no-common-ligatures;"><span style="font-size:10px;">
</span></span></p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo; color: rgb(201, 27, 19);"><span style="font-variant-ligatures: no-common-ligatures;"><span style="font-size:10px;">设置属性:</span></span></p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo; color: rgb(201, 27, 19);"><span style="font-size:10px;"><span style="font-variant-ligatures: no-common-ligatures;"></span></span></p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo; color: rgb(195, 34, 117);"><span style="font-size:10px;"><span style="font-variant-ligatures: no-common-ligatures;">@property</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);">(</span><span style="font-variant-ligatures: no-common-ligatures;">nonatomic</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);">, </span><span style="font-variant-ligatures: no-common-ligatures;">strong</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);">)</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(97, 34, 174);">AVPlayer</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);"> *player;</span></span></p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo; color: rgb(195, 34, 117);"><span style="font-size:10px;"><span style="font-variant-ligatures: no-common-ligatures;">@property</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);">(</span><span style="font-variant-ligatures: no-common-ligatures;">nonatomic</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);">, </span><span style="font-variant-ligatures: no-common-ligatures;">strong</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);">)</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(97, 34, 174);">AVPlayerItem</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);"> *playerItem;</span></span></p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo; color: rgb(195, 34, 117);"><span style="font-size:10px;"><span style="font-variant-ligatures: no-common-ligatures;">@property</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);">(</span><span style="font-variant-ligatures: no-common-ligatures;">nonatomic</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);">, </span><span style="font-variant-ligatures: no-common-ligatures;">strong</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);">)</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(97, 34, 174);">AVAsset</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);"> *asset;</span></span></p><p></p>

 
ViewDidLoad 里面:
//将网址进行 UTF8 转码,避免有些汉字会变乱码
    NSString *urlStr = [URLSTR_LIUCHANG stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    NSString *urlStr = [URLSTR_KA stringByAddingPercentEncodingWithAllowedCharacters:[[NSCharacterSet characterSetWithCharactersInString:URLSTR_KA] invertedSet]];

    
    //构造AVPlayer
    _asset = [AVAsset assetWithURL:[NSURL URLWithString:urlStr]];
    _playerItem = [AVPlayerItem playerItemWithAsset:_asset];
    _player = [[AVPlayer alloc]initWithPlayerItem:_playerItem];
    
    //创建 playerLayer 图层
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
    //添加到View图层
    [self.view.layer addSublayer:playerLayer];
    
    //播放
    [_player play];
    
    //设置 playerLayer 的 fram
    playerLayer.frame = self.view.bounds;
    
    
    //监听PlayerItem这个类
    [self.playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
    [self.playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
    [self.playerItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil];
    [self.playerItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil];


做了以上操作后就可以监听了,实现下面方法来监听

//监听获得消息
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    {
        AVPlayerItem *playerItem = (AVPlayerItem *)object;
        
        if ([keyPath isEqualToString:@"status"]) {
            if ([playerItem status] == AVPlayerStatusReadyToPlay) {
                //status 点进去看 有三种状态
                
                CGFloat duration = playerItem.duration.value / playerItem.duration.timescale; //视频总时间
                NSLog(@"准备好播放了,总时间:%.2f", duration);//还可以获得播放的进度,这里可以给播放进度条赋值了
                
            } else if ([playerItem status] == AVPlayerStatusFailed || [playerItem status] == AVPlayerStatusUnknown) {
                [_player pause];
            }
            
        } else if ([keyPath isEqualToString:@"loadedTimeRanges"]) {  //监听播放器的下载进度
            
            NSArray *loadedTimeRanges = [playerItem loadedTimeRanges];
            CMTimeRange timeRange = [loadedTimeRanges.firstObject CMTimeRangeValue];// 获取缓冲区域
            float startSeconds = CMTimeGetSeconds(timeRange.start);
            float durationSeconds = CMTimeGetSeconds(timeRange.duration);
            NSTimeInterval timeInterval = startSeconds + durationSeconds;// 计算缓冲总进度
            CMTime duration = playerItem.duration;
            CGFloat totalDuration = CMTimeGetSeconds(duration);
            
            NSLog(@"下载进度:%.2f", timeInterval / totalDuration);
            
        } else if ([keyPath isEqualToString:@"playbackBufferEmpty"]) { //监听播放器在缓冲数据的状态
                
            NSLog(@"缓冲不足暂停了");
            
            
        } else if ([keyPath isEqualToString:@"playbackLikelyToKeepUp"]) {
            
            NSLog(@"缓冲达到可播放程度了");
            
            //由于 AVPlayer 缓存不足就会自动暂停,所以缓存充足了需要手动播放,才能继续播放
            [_player play];
            
        }
    }
}




  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值