我的应用程序正在使用AVPlayerLayer来显示AVPictureInPictureController,我正在使用KVO来观察有关AVPlayerLayer的属性。 pictureInPictureControllerDidStopPictureInPicture
,在此方法中,我移除了playerView
,并在dealloc
中删除了观察者
当我停止PictureInPicture
并返回起居室时,它会崩溃;控制台日志:
0 QuartzCore!CA::Layer::~Layer() + 0xfc
1 QuartzCore!CA::Layer::~Layer() + 0xc8
2 QuartzCore!-[CALayer dealloc] + 0x48
3 AVFoundation!-[AVPlayerLayer dealloc] + 0x280
4 Foundation!NSKVODeallocate + 0x58
5 QuartzCore!CA::Layer::free_transaction(CA::Transaction*) + 0xb0
6 QuartzCore!CA::Transaction::commit() + 0x27c
7 QuartzCore!CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 0x74
8 CoreFoundation!__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 0x1c
9 CoreFoundation!__CFRunLoopDoObservers + 0x170
0 libsystem_platform.dylib!_os_unfair_lock_unowned_abort + 0x24
1 libsystem_platform.dylib!_os_unfair_lock_unlock_slow + 0x78
2 libsystem_malloc.dylib!free_tiny_botch + 0x5c
3 QuartzCore!-[CALayer dealloc] + 0x54
4 AVFoundation!-[AVPlayerLayer dealloc] + 0x280
5 Foundation!NSKVODeallocate + 0x58
6 QuartzCore!CA::Layer::free_transaction(CA::Transaction*) + 0xb0
7 QuartzCore!CA::Transaction::commit() + 0x27c
8 QuartzCore!CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 0x74
9 CoreFoundation!__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 0x1c
我添加KVO的方法
- (void)addObserverToPlayerItem:(AVPlayerItem *)playerItem
{
[playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:@"PIPViewControllerClass"];
[playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:@"PIPViewControllerClass"];
[playerItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:@"PIPViewControllerClass"];
[playerItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:@"PIPViewControllerClass"];
[playerItem addObserver:self forKeyPath:@"presentationSize" options:NSKeyValueObservingOptionNew context:@"PIPViewControllerClass"];
}
- (void)addObserverToPlayer:(AVPlayer *)player
{
[player addObserver:self forKeyPath:@"rate" options:NSKeyValueObservingOptionNew context:@"PIPViewControllerClass"];
}
在这个方法中我改变了项目并添加KVO pictureInPictureControllerDidStartPictureInPicture:
在此方法中我删除了视图 pictureInPictureControllerDidStopPictureInPicture:
当视图为dealloc时,我删除了KVO
- (void)removeObserverFromPlayerItem:(AVPlayerItem *)playerItem
{
[playerItem removeObserver:self forKeyPath:@"status" context:@"PIPViewControllerClass"];
[playerItem removeObserver:self forKeyPath:@"loadedTimeRanges" context:@"PIPViewControllerClass"];
[playerItem removeObserver:self forKeyPath:@"playbackBufferEmpty" context:@"PIPViewControllerClass"];
[playerItem removeObserver:self forKeyPath:@"playbackLikelyToKeepUp" context:@"PIPViewControllerClass"];
[playerItem removeObserver:self forKeyPath:@"presentationSize" context:@"PIPViewControllerClass"];
}
- (void)removeObserverToPlayer:(AVPlayer *)player
{
[player removeObserver:self forKeyPath:@"rate" context:@"PIPViewControllerClass"];
}