【发布时间】:2012-09-26 22:00:08
【问题描述】:
一个 AVPlayerItem 只能分配给一个 AVPlayer。将 AVPlayerItem 添加到 AVPlayer 后,以后尝试将其添加到不同的 AVPlayer 将 SIGABRT 应用程序。
那么,给定一个 AVPlayerItem,你如何确定:
- 它当前与哪个 AVPlayer 关联?和
- 过去是否曾将其插入 AVPlayer?
以下代码可靠地演示了该问题:
AVPlayerItem *item = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"]];
AVPlayer *firstPlayer = [[AVPlayer alloc] init];
[firstPlayer replaceCurrentItemWithPlayerItem:item];
AVPlayer *secondPlayer = [[AVPlayer alloc] init];
[secondPlayer replaceCurrentItemWithPlayerItem:item];
这是错误信息:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'An AVPlayerItem cannot be associated with more than one instance of AVPlayer'
【问题讨论】:
-
你找到答案了吗?
-
哈,不。我们采取了不同的方法。当我们准备将可疑血统的 AVPlayerItem 分配给 AVPlayer 时,我们克隆该项目并广播一条消息,我们正在用新项目替换旧项目。我们还使用关联对象将旧对象指向新对象。因此,如果我们得到一个旧的 AVPlayerItem 已经发生了这个过程,我们可以按照前向关联对象指针的链表找到最终的“真正的”。谢谢苹果。