2

I am working on a custom video player using AVPlayer. I load videos stored on the local file system in the Cache folder (NSCache). I initialize the player like this:

self.playerItem = [[AVPlayerItem alloc] initWithURL:self.localURL];
[self.playerItem addObserver:self forKeyPath:NSStringFromSelector(@selector(status)) options:NSKeyValueObservingOptionInitial context:nil];
self.avPlayer = [[AVPlayer alloc]initWithPlayerItem:self.playerItem];
[self.avPlayer addObserver:self forKeyPath:NSStringFromSelector(@selector(status)) options:NSKeyValueObservingOptionInitial context:nil];

This generally works fine. However I have frequent fails on the status of the AVPlayerItem with this error:

NSLocalizedDescription = "The operation could not be completed";
NSLocalizedFailureReason = "An unknown error occurred (-12983)";
NSUnderlyingError = "Error Domain=NSOSStatusErrorDomain Code=-12983

The strange thing is that the same URLs which fail sometimes work just shortly after and before such fails. I would say every 10th load attempt fails. I can't figure out what causes this or where to look for answers. A search for the error code turned up empty for me. Any help or pointers are highly appreciated.

Andi S.
  • 163
  • 2
  • 11
  • you can reference to this answer: http://stackoverflow.com/questions/22178444/can-t-observe-avplayeritem-for-status-key – vien vu Dec 31 '15 at 04:45
  • @vienvu Thanks a lot for the hint, but this is not related. The observing part does work and the player item does change its status unlike in this question. The status has an error object attached to it and I am searching for the cause of this error. – Andi S. Dec 31 '15 at 04:49
  • Can you check that: controller you play video which have deallocated? – vien vu Dec 31 '15 at 04:51
  • Not sure exactly what you mean. If the controller from which I play the video has been deallocated? The controller and player are all still there and have not been deallocated at the time of the error. – Andi S. Dec 31 '15 at 04:57
  • ah I mean that sometimes the controller don't deallocated so observer you are registered still there and it maybe cause issue. so make sure you remove all observer after you don't use. And check controller play it deallocated after you don't use it. – vien vu Dec 31 '15 at 04:58
  • Can you post more code? Why are you observing status of player AND player item? – ChrisH Dec 31 '15 at 16:09
  • @vienvu thanks for the tip. I do actually remove the kvo observers in dealloc, but good advice nonetheless. – Andi S. Dec 31 '15 at 18:41
  • @ChrisH I observe the status of both to get more feedback on where something fails in case it does. Do you think that is wrong or could cause issues? Didn't see anything in the docs suggesting you can't keep track of both objects. I will clean up my code a little and post more. – Andi S. Dec 31 '15 at 18:44

1 Answers1

3

After a lengthy hunt, I was able to track down the source of the issue. The problem was an undocumented limit on the number of AVPlayer items which can exist concurrently. If there are too many instances videos can no longer be loaded failing with the AVPlayerItem error -12983.

Other people seem to have run into the same issue as well: AVPlayerItemStatusFailed error when too many AVPlayer instances created. I solved the issue by reusing player instances and making sure that there are not too many active ones at the same time.

Community
  • 1
  • 1
Andi S.
  • 163
  • 2
  • 11