AVPlayerItem was deallocated while key value observers were still registered with it

810 views Asked by At

In viewDidAppear I am registering for observers:

[self.avPlayer addObserver:self forKeyPath:@"rate" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
isRate = YES;

[playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
isStatus = YES;

[playerItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
isPlayback = YES;

In viewDidDisappear, I am de-registering very safely (to avoid any exceptions)

@try {
    if (isStatus && self.avPlayer && self.avPlayer.currentItem) {
        [self.avPlayer.currentItem removeObserver:self forKeyPath:@"status"];
    }
    if (isPlayback && self.avPlayer && self.avPlayer.currentItem) {
        [self.avPlayer.currentItem removeObserver:self forKeyPath:@"playbackBufferEmpty"];
    }
    if (isRate && self.avPlayer) {
        [self.avPlayer removeObserver:self forKeyPath:@"rate"];
    }
} @catch (NSException *ex) {
    NSLog(@"Exception thrown while removing observer(s) %@", ex);
}

Even after taking all the precautions, I get application crash and on new relic I see

An instance 0x1742046c0 of class AVPlayerItem was deallocated while key value observers were still registered with it. Current observation info: ( Context: 0x0, Property: 0x174643810> Context: 0x0, Property: 0x17465f800> )

Is there anything else I can do to avoid the crash?

What I am doing? I am sliding the video player to move onto another video player. so there are chances that the keys don't get registered in the first place.

Declaration of player item:

AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:avAsset];
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
self.avPlayer = [AVPlayer playerWithPlayerItem:playerItem];
0

There are 0 answers