您好,我正在使用 AVPlayer 在 UITableViewCells 上播放视频,它在 iOS 7 上工作正常,但在 iOS8 中崩溃并出现以下错误。
'An instance 0x7c01b000 of class AVPlayerItem was deallocated while key value observers were still registered with it.
这是我的代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
.........
.........
if(cell.videoPlayer!= nil && cell.videoPlayer.currentItem != nil)
{
[cell.videoItem removeObserver:self forKeyPath:@"playbackBufferEmpty" context:nil];
[cell.videoItem removeObserver:self forKeyPath:@"playbackLikelyToKeepUp" context:nil];
}
cell.videoPlayer = [AVPlayer playerWithPlayerItem:cell.videoItem];
cell.avLayer = [AVPlayerLayer playerLayerWithPlayer:cell.videoPlayer];
cell.videoPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[cell.videoItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionInitial context:nil];
[cell.videoItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionInitial context:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidBufferPlaying:) name:AVPlayerItemPlaybackStalledNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
cell.avLayer.frame = CGRectMake(5, 9, 310, 310);
[cell.contentView.layer addSublayer: cell.avLayer];
[ cell.videoPlayer play];
[cell.contentView addSubview:cell.videoActivity];
return cell;
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context {
NSArray* cells = homeTabl.visibleCells;
for (HomeCell* cell in cells) {
if (object == cell.videoItem && [keyPath isEqualToString:@"playbackBufferEmpty"]) {
if (cell.videoItem.playbackBufferEmpty) {
NSLog(@"buffering");
cell.videoActivity.hidden = NO;
}
}
else if (object == cell.videoItem && [keyPath isEqualToString:@"playbackLikelyToKeepUp"])
{
if (cell.videoItem.playbackLikelyToKeepUp)
{
cell.videoActivity.hidden = YES;
[cell.videoPlayer play];
}
}
}
}
-(void)scrollViewDidScroll:(UIScrollView *)aScrollView {
NSArray* cells = homeTabl.visibleCells;
for (HomeCell* cell in cells) {
[cell.videoPlayer pause];
[cell.avLayer removeFromSuperlayer];
cell.videoPlayer = nil;
cell.videoItem = nil;
}
可能是什么原因 ?我已经解决了这个问题,但我无法在我的代码中实现它。请帮我解决这个问题。
根据我的经验,删除观察者是 iOS 中最不稳定的领域之一。您需要非常小心地平衡对 AddObserver 和 RemoveObserver 的调用。我找到了一种故障安全方法来执行此操作,即将任何 AddObserver 调用放入对象的 Init 方法中,然后通过其 Dealloc 方法中的 RemoveObserver 调用来平衡这些调用。在您的情况下,这将位于您的“videoItem”子类中。 (此代码未检查)
- (id) initWithOwner:(id)owner
{
self = [super init];
if( self ) {
_owner = owner;
[self addObserver:_owner forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionInitial context:nil];
}
return self;
}
- (void) dealloc
{
[self removeObserver:_owner forKeyPath:@"playbackBufferEmpty" context:nil];
}
我不确定 videoItem 是在哪里声明的,但基本上您创建了一个名为 VideoItem 的新类,并在其中创建了一个名为 initWithOwner: 的新初始化程序。在 cellForRowAtIndexPath: 方法中,当您创建新单元格时,您还创建了一个 VideoItem 实例并传入 self 作为所有者
self.videoItem = [[VideoItem alloc] initWithOwner:self];
如果没有更多的代码,我无法真正更详细地指定这一点。您可能还会考虑首先在 xcode 中格式化代码,然后将其剪切并粘贴到 SO 中以保持整洁。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)