iOS彻底删除播放器AVPlayer

关于删除视频播放的问题困扰我一天多时间,写下来记录下!
公司需求类似播放微信朋友圈小视频,只要播放器,点击全屏播放,再次点击移除掉。
在这个频繁的点击添加删除,一旦不移除干净,真的是各种问题
创建的对象如下
@property (nonatomic, strong) AVPlayerLayer *playerLayer;
@property (nonatomic, strong) AVPlayer *player;
这两个必不可少,其他根据需求自行添加
移除的时候,移除自己添加的通知
[self.playerItem removeObserver:self forKeyPath:@"loadedTimeRanges"];
[self.playerItem removeObserver:self forKeyPath:@"playbackBufferEmpty"];
[self.playerItem removeObserver:self forKeyPath:@"playbackLikelyToKeepUp"];
[[NSNotificationCenter defaultCenter]removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:[self.player currentItem]];
移除对象
if (self.player) {
[self pause];
self.playerItem = nil;
self.videoMaskView = nil;
self.currentPlayTime = 0;
[self.player.currentItem cancelPendingSeeks];
[self.player.currentItem.asset cancelLoading];
[self.playerLayer removeFromSuperlayer];
self.totalTime = 0;
self.player = nil;
self.playerLayer = nil;
[self.videoMaskView removeFromSuperview];
[self removeFromSuperview];
}
重点之重~~~~~下边这俩,不然添加的时候,冷不丁的就会出问题
[self.playerLayer removeFromSuperlayer];
self.playerLayer = nil;

你可能感兴趣的:(iOS彻底删除播放器AVPlayer)