音视频再学习笔记(2)(about AVPlayerItem)

文档地址:

https://developer.apple.com/reference/avfoundation/avplayeritem

1AVPlayerItem is a dynamic object.In addition to its property values that can be changed by you ,many of its read-only property values can be changed by the associated AVPlayer during the item’s preparation and playback.

AVPlayerItem 是一个动态的对象,除了它的属性值能被你改变之外,它的只读属性值在这个item的准备和播放的中能被相关的AVPlayer改变。

2、You can use key-value observing to observe these state changes as they occur.One of the most important player item properties to observe is its status.

监听中最重要的状态时status.

3、The status indicates if the item is ready for playback and generally available for use.When you first create a player item, its status has a value of unknown ,meaning its media hasn’t been loaded and has not yet been enqueued for playback.Associating a player item with an AVPlayer immediately begins  enqueuing the item’s media and preparing it for playback, but you need to wait until its status changes to readyToPlay before it’s ready for use.

这个状态显示这个item是否准备播放或者适合使用。当你最初创建的时候,它的状态是一个unknown值,意味着它的媒体没有被加载,也还没有入队播放,将item 和AVplayer连接会立刻开始入队item的媒体项目,并且准备播放,但是你需要等到状态变为readyToPlay才行。

4、you can use the methods of the AVAsynchronousKeyValueLoading protocol to load the values you need .Alternatively ,AVPlayerItem can automatically load the needed asset data for you by passing the desired set of keys to its init(asset:automticallyLoadedAssetKeys:) initializer

你可以用AVAsynchronousKeyValueLoading的协议方法来加载你需要的值,另外,AVPlayerItem通过初始化方法init...来自动加载需要的asset data.

5、func prepareToPlay() {

    let url = <#Asset URL#> 

    asset = AVAsset(url: url)


   let assetKeys = [

        “playable”,

         “hasProtectedContent”

    ]

    playerItem = AVPlayerItem(asset: asset, automaticallyLoadedAssetKeys: assetKeys)

//Register as an observer of the player item’s status property 

   playerItem.addObserver(self,forKeyPath: #keyPath(AVPlayItem,status),options: [.old, .new], context: &playerItemContext)

//player = AVPlayer(playerItem: playerItem)

}

6、You should call this method before associating the player item with the player to make sure you capture all state changes to the item’s status.

你应该在item和player关联之前调用这个观察者方法,这样确保你能俘获关于item的状态的所有改变。

7、To be notified of changes to the status ,you need to implement the 

observaValue(forkeyPath:of :change:context) method.This method is invoked whenever the status changes giving you the chance to take some action in response.

为了知道状态的改变,你需要实现observaValue方法,这个方法被调用,当状态改变的时候给你一个机会在反馈中做一些行动。










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值