ZFPlayer 在UITableView中的使用
2018-10-19 本文已影响0人
低调的前行者灬
前言
ZFPlayer demo中已经列出各种场景的使用,由于公司项目类似于微博,不只是有视频的展示,tableview由多种复杂cell组成,为了完美适配,所以更改了部分源代码
ZFPlayer地址
正文
1. 初始化播放器,设置属性,回调等
ZFPlayerController初始化要给定对应的View的tag,这个View一般是UIIMageView用来在cell中展示封面的,ZFPlayer内部完全是根据tag查找View来进行设置播放视图的
ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
self.player = [ZFPlayerController playerWithScrollView:self.storyTableView playerManager:playerManager containerViewTag:999];
self.player.controlView = self.controlView;
此处的controlView即ZFPlayerControlView,它有两个关键子视图ZFPortraitControlView和ZFLandScapeControlView,分别是竖屏全屏和横屏全屏的播放视图
ZFPlayerController可以设置很多属性,可以选择移动网络是否自动播放,也可以直接控制是否自动播放
// 移动网络依然自动播放(默认NO)
self.player.WWANAutoPlay = NO;
//自动播放
self.player.shouldAutoPlay = NO;
播放停止之后重置播放器
MyWeakObj(self);
self.player.playerDidToEnd = ^(id _Nonnull asset) {
[selfWeak.player stopCurrentPlayingCell];
};
旋转屏幕做的处理
self.player.orientationWillChange = ^(ZFPlayerController * _Nonnull player, BOOL isFullScreen) {
[selfWeak setNeedsStatusBarAppearanceUpdate];
[UIViewController attemptRotationToDeviceOrientation];
selfWeak.storyTableView.scrollsToTop = !isFullScreen;
};
播放时间大于0.2隐藏视频封面,也即之前设置的tag对应的View
这样的处理的原因在于,播放器在开始播放之后会有短暂的黑屏过程,主要是由于开始播放之后ZFPlayerLoadState状态为ZFPlayerLoadStatePlaythroughOK,但是存在短暂的解析过程,视频的第一帧并没有出现
self.player.playerPlayTimeChanged = ^(id<ZFPlayerMediaPlayback> _Nonnull asset, NSTimeInterval currentTime, NSTimeInterval duration) {
DLog(@"当前播放时间=%f",currentTime);
if (currentTime > 0.2) {
selfWeak.controlView.coverImageView.hidden = YES;
}
};
2.屏幕旋转处理
对于包含UITabbarController和UINavigationController的控制器,需要分别实现三个方法
UITabbarController
- (BOOL)shouldAutorotate
{
return [self.selectedViewController shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations
{
return [self.selectedViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}
UINavigationController
- (BOOL)shouldAutorotate {
return [self.topViewController shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return [self.topViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
对于父控制器包含子控制器的情况,如果在子控制器中有旋转屏幕的需求,那么父控制器也要实现shouldAutorotate方法
3.滑动播放当前正在显示的cell
需要实现下面几个方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[scrollView zf_scrollViewDidEndDecelerating];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
[scrollView zf_scrollViewDidEndDraggingWillDecelerate:decelerate];
}
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
[scrollView zf_scrollViewDidScrollToTop];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[scrollView zf_scrollViewDidScroll];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[scrollView zf_scrollViewWillBeginDragging];
}
zf_scrollViewDidStopScrollCallback这个回调可以返回当前正在显示的cell,实现播放方法即可
// 停止的时候找出最合适的播放
MyWeakObj(self);
_storyTableView.zf_scrollViewDidStopScrollCallback = ^(NSIndexPath * _Nonnull indexPath) {
[selfWeak playTheVideoAtIndexPath:indexPath scrollToTop:NO];
};
问题
: ZFPLayer写的关于寻找当前正在播放的cell本身是没有问题的,但是只是不适合我公司的项目而已,在UIScrollView+ZFPlayer.m的zf_filterShouldPlayCellWhileScrolling方法中,根据tag查找到对应View之后只做了是否存在的判断,而我的逻辑是隐藏,所以计算的当前显示的cell,总是会有问题