我是靠谱客的博主 寂寞小蚂蚁,最近开发中收集的这篇文章主要介绍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];
            
        }
    }
}




最后

以上就是寂寞小蚂蚁为你收集整理的AVPlayer的一些基本用法,缓存监听等的全部内容,希望文章能够帮你解决AVPlayer的一些基本用法,缓存监听等所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部