Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the AVPlayer associated with an AVPlayerItem

Tags:

ios

avplayer

An AVPlayerItem can only ever be assigned to one AVPlayer. Once an AVPlayerItem has been added to an AVPlayer, future attempts to add it to a different AVPlayer will SIGABRT the app.

So, given an AVPlayerItem, how can you determine:

  1. What AVPlayer it is currently associated with? and
  2. If it has ever been inserted into an AVPlayer at any point in the past?

The following code demonstrates the problem reliably:

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];

And here's the error message:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'An AVPlayerItem cannot be associated with more than one instance of AVPlayer'
like image 643
jimbo Avatar asked Sep 26 '12 22:09

jimbo


1 Answers

There is a semi-legal way that might break in the future. The player item has a private method (and, it seems, variable) named _player. You can query it like this (which technically doesn't use private API, so I think it should be AppStore-safe):

id player = [item valueForKey:@"player"];
like image 196
DarkDust Avatar answered Oct 02 '22 17:10

DarkDust