找到与AVPlayerItem关联的AVPlayer

jim*_*mbo 15 ios avplayer

AVPlayerItem只能分配给一个AVPlayer.将AVPlayerItem添加到AVPlayer后,将来尝试将其添加到其他AVPlayer将SIGABRT应用程序.

那么,给定AVPlayerItem,您如何确定:

  1. 它目前与哪个AVPlayer相关联?和
  2. 如果它曾经在过去的任何一点插入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];
Run Code Online (Sandbox Code Playgroud)

这是错误信息:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'An AVPlayerItem cannot be associated with more than one instance of AVPlayer'
Run Code Online (Sandbox Code Playgroud)

Dar*_*ust 6

有一种半合法的方式,将来可能会中断。播放器项具有名为的私有方法(似乎是变量)_player。您可以这样查询(从技术上讲,它不使用私有API,因此我认为它应该是AppStore安全的):

id player = [item valueForKey:@"player"];
Run Code Online (Sandbox Code Playgroud)


Ani*_*nil 3

你尝试过相反的方法吗?尝试访问 AVPlayer 正在播放的项目内的详细信息

AVPlayer *firstPlayer = [[AVPlayer alloc]init];
Run Code Online (Sandbox Code Playgroud)

之后您就可以访问firstPlayer.currentItem。这将使您可以firstPlayer访问AVPlayerItem

例如:

firstPlayer.currentItem.duration
Run Code Online (Sandbox Code Playgroud)

会给你该曲目的持续时间。