4

If I use KVO to observe my player items like this:

    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
    [playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:MyClassKVOContext];
    [playerItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:MyClassKVOContext];
    [playerItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:MyClassKVOContext];
    [playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:MyClassKVOContext];

Two questions:

1) Do I need to remove my observers once the item finishes playing? (i.e. in AVPlayerItemDidPlayToEndTimeNotification)

2) If I call [_avQueuePlayer removeAllItems] does it also remove each item's observers?

Oren
  • 5,055
  • 3
  • 34
  • 52

1 Answers1

1

make your player object(AVPlayerItem *playerItem) Globally and set its property and synthesize it.

1) Do I need to remove my observers once the item finishes playing? (i.e. in AVPlayerItemDidPlayToEndTimeNotification)

Yes, your need to remove all observer when you are about to leave that view controller where that player is playing. Not after finishes playing.

2) If I call [_avQueuePlayer removeAllItems] does it also remove each item's observers?

Yes, it remove all observer that are set for that global variable.

Deepak
  • 1,421
  • 1
  • 16
  • 20
  • You can also use AVPlayerItem removeObserver:forKeyPath method for removing observer for particular key. – Deepak Sep 16 '14 at 10:22
  • I have multiple AVPlayerItem globally since it plays across multiple viewcontrollers. If I call removeAllItems on the player, it removes the observers? However by default when an item finishes playing it does not? – Oren Sep 16 '14 at 19:15