ZFPlayer 3.0解析
此博客详细介绍一下ZFPlayer 3.0的用法,如果你有什么问题或者建议可联系我。在此之前你使用ZFPlayer,是不是在烦恼播放器SDK自定义,控制层自定义等等问题。笔者公司多个项目分别使用不同播放器SDK和每个项目控制层都不一样,但是为了统一管理、统一调用和各种自定义,我特意写了这个播放器壳子,意在解决各种痛点。播放器SDK只要遵守ZFPlayerMediaPlayback
协议,控制层只要遵守ZFPlayerMediaControl
协议,可以实现自定义播放器和控制层。
目前支持的功能如下:
- 普通模式的播放,类似于腾讯视频、爱奇艺等APP;
- 列表普通模式的播放,包括手动点击播放、滑动到屏幕中间自动播放,wifi网络智能播放等等;
- 列表的亮暗模式播放,类似于微博、UC浏览器视频列表等APP;
- 列表视频滑出屏幕后停止播放、滑出屏幕后小窗播放;
- 优雅的全屏,支持横屏和竖屏全屏模式;
播放器的主要类为ZFPlayerController
,具体API请看下边这张图吧,后边我也会详细介绍。在之前版本收到好多开发朋友的Issues建议也好bug也好,ZFPlayer也是致力于解决这些问题和满足各位的建议。
ZFPlayerController(播放器的主要类)
初始化方式:
/// 普通播放的初始化
+ (instancetype)playerWithPlayerManager:(id<ZFPlayerMediaPlayback>)playerManager containerView:(UIView *)containerView;
/// 普通播放的初始化
- (instancetype)initWithPlayerManager:(id<ZFPlayerMediaPlayback>)playerManager containerView:(UIView *)containerView;
/// 列表播放的初始化
+ (instancetype)playerWithScrollView:(UIScrollView *)scrollView playerManager:(id<ZFPlayerMediaPlayback>)playerManager containerViewTag:(NSInteger)containerViewTag;
/// 列表播放的初始化
- (instancetype)initWithScrollView:(UIScrollView *)scrollView playerManager:(id<ZFPlayerMediaPlayback>)playerManager containerViewTag:(NSInteger)containerViewTag;
属性
/// 初始化时传递的容器视图,用来显示播放器view,和播放器view同等大小
@property (nonatomic, readonly) UIView *containerView;
/// 初始化时传递的播放器manager,必须遵守`ZFPlayerMediaPlayback`协议
@property (nonatomic, readonly) id<ZFPlayerMediaPlayback> currentPlayerManager;
/// 此属性是设置显示的控制层,自定义UIView遵守`ZFPlayerMediaControl`协议,实现相关协议就可以满足自定义控制层的目的。
@property (nonatomic, strong) UIView<ZFPlayerMediaControl> *controlView;
ZFPlayerController (ZFPlayerTimeControl)
/// 当前播放视频的时间,支持KVO
@property (nonatomic, readonly) NSTimeInterval currentTime;
/// 当前播放视频总时间,支持KVO
@property (nonatomic, readonly) NSTimeInterval totalTime;
/// 当前播放视频缓冲时间,支持KVO
@property (nonatomic, readonly) NSTimeInterval bufferTime;
/// 根据播放的时间和总时间,计算出当前播放的进度,取值范围0...1
@property (nonatomic, readonly) float progress;
/// 根据播放缓冲时间和总时间,计算出当前缓冲的进度,取值范围0...1
@property (nonatomic, readonly) float bufferProgress;
/// 调节播放进度
- (void)seekToTime:(NSTimeInterval)time completionHandler:(void (^ __nullable)(BOOL finished))completionHandler;
ZFPlayerController (ZFPlayerPlaybackControl)
/// 0...1.0,调节系统的声音,要是调节播放器声音可以使用播放器管理类设置
@property (nonatomic) float volume;
/// 系统静音,要是调节播放器静音可以使用播放器管理类设置
@property (nonatomic, getter=isMuted) BOOL muted;
// 0...1.0, 系统屏幕亮度
@property (nonatomic) float brightness;
/// 移动网络下自动播放, default is NO.
@property (nonatomic, getter=isWWANAutoPlay) BOOL WWANAutoPlay;
/// 当前播放的下标,只适用于设置了`assetURLs`
@property (nonatomic) NSInteger currentPlayIndex;
/// 当退到后台后是否暂停播放,前提是支持后台播放器模式,default is YES.
@property (nonatomic) BOOL pauseWhenAppResignActive;
/// 播放完的回调
@property (nonatomic, copy, nullable) void(^playerDidToEnd)(id asset);
/// 播放下一个,只适用于设置了`assetURLs`
- (void)playTheNext;
/// 播放上一个,只适用于设置了`assetURLs`
- (void)playThePrevious;
/// 播放某一个,只适用于设置了`assetURLs`
- (void)playTheIndex:(NSInteger)index;
/// 停止播放,并且把播放器view和相关通知移除
- (void)stop;
/// 切换当前的PlayerManager,适用场景:播放某一个视频时候使用特定的播放器管理类
- (void)replaceCurrentPlayerManager:(id<ZFPlayerMediaPlayback>)manager;
ZFPlayerController (ZFPlayerOrientationRotation)
/// 屏幕旋转管理类
@property (nonatomic, readonly) ZFOrientationObserver *orientationObserver;
/// 是否是全屏状态,当ZFFullScreenMode == ZFFullScreenModeLandscape,当currentOrientation是LandscapeLeft或者LandscapeRight,这个值是YES
/// 当ZFFullScreenMode == ZFFullScreenModePortrait,当视频全屏后,这个值是YES
@property (nonatomic, readonly) BOOL isFullScreen;
/// 锁定当前的屏幕方向,目的是禁止设备自动旋转
@property (nonatomic, getter=isLockedScreen) BOOL lockedScreen;
/// 隐藏系统的状态栏
@property (nonatomic, getter=isStatusBarHidden) BOOL statusBarHidden;
/// 播放器view当前方向
@property (nonatomic, readonly) UIInterfaceOrientation currentOrientation;
/// 当即将全屏时候会调用
@property (nonatomic, copy, nullable) void(^orientationWillChange)(ZFPlayerController *player, BOOL isFullScreen);
/// 当已经全屏时候会调用
@property (nonatomic, copy, nullable) void(^orientationDidChanged)(ZFPlayerController *player, BOOL isFullScreen);
/// 添加设备方向的监听
- (void)addDeviceOrientationObserver;
/// 移除设备方向的监听
- (void)removeDeviceOrientationObserver;
/// 当 ZFFullScreenMode == ZFFullScreenModeLandscape使用此API设置全屏切换
- (void)enterLandscapeFullScreen:(UIInterfaceOrientation)orientation animated:(BOOL)animated;
/// 当 ZFFullScreenMode == ZFFullScreenModePortrait使用此API设置全屏切换
- (void)enterPortraitFullScreen:(BOOL)fullScreen animated:(BOOL)animated;
/// 内部根据ZFFullScreenMode的值来设置全屏切换
- (void)enterFullScreen:(BOOL)fullScreen animated:(BOOL)animated;
ZFPlayerController (ZFPlayerViewGesture)
/// 手势的管理类
@property (nonatomic, readonly) ZFPlayerGestureControl *gestureControl;
/// 禁用哪些手势,默认支持单击、双击、滑动、缩放手势
@property (nonatomic, assign) ZFPlayerDisableGestureTypes disableGestureTypes;
ZFPlayerController (ZFPlayerScrollView)
/// 初始化时候设置的scrollView
@property (nonatomic, readonly, nullable) UIScrollView *scrollView;
/// 列表播放时候是否自动播放,default is YES.
@property (nonatomic) BOOL shouldAutoPlay;
/// 列表播放的时小屏的悬浮窗,如果滑出屏幕后小窗播放,需要设置次view的默认frame,次view支持在屏幕可见范围内随意拖动。
@property (nonatomic, readonly, nullable) ZFFloatView *smallFloatView;
/// 当前播放的indexPath
@property (nonatomic, nullable) NSIndexPath *playingIndexPath;
/// 初始化时候设置的containerViewTag,根据此tag在cell上找到播放器view显示的位置
@property (nonatomic) NSInteger containerViewTag;
/// 滑出屏幕后是否停止播放,如果设置为NO,滑出屏幕后则会小窗播放,defalut is YES.
@property (nonatomic) BOOL stopWhileNotVisible;
/// 小屏悬浮窗是否正在显示
@property (nonatomic, readonly) BOOL isSmallFloatViewShow;
/// 如果列表播放时候有一个区或者普通播放时候,可以使用此API,此时 `playTheNext` `playThePrevious` `playTheIndex:`有效。
@property (nonatomic, copy, nullable) NSArray <NSURL *>*assetURLs;
/// 如果列表播放时候有多个区,使用此API
@property (nonatomic, copy, nullable) NSArray <NSArray <NSURL *>*>*sectionAssetURLs;
/// 在cell上停止当前正在播放的视频
- (void)stopCurrentPlayingCell;
//// 设置播放某indexPath,是否把当前播放indexPath滑动到UICollectionViewScrollPositionTop位置
- (void)playTheIndexPath:(NSIndexPath *)indexPath scrollToTop:(BOOL)scrollToTop;
ZFPlayerMediaPlayback—播放器SDK遵守的协议
- 枚举类型:
/// 播放状态:未知、播放中、暂停、失败、停止
typedef NS_ENUM(NSUInteger, ZFPlayerPlaybackState) {
ZFPlayerPlayStateUnknown = 0,
ZFPlayerPlayStatePlaying,
ZFPlayerPlayStatePaused,
ZFPlayerPlayStatePlayFailed,
ZFPlayerPlayStatePlayStopped
};
/// 加载状态:未知、就绪、可以播放、自动播放、播放暂停
typedef NS_OPTIONS(NSUInteger, ZFPlayerLoadState) {
ZFPlayerLoadStateUnknown = 0,
ZFPlayerLoadStatePrepare = 1 << 0,
ZFPlayerLoadStatePlayable = 1 << 1,
ZFPlayerLoadStatePlaythroughOK = 1 << 2,
ZFPlayerLoadStateStalled = 1 << 3,
};
/// 播放画面拉伸模式:无拉伸、等比例拉伸不裁剪、部分内容裁剪按比例填充、非等比例填满
typedef NS_ENUM(NSInteger, ZFPlayerScalingMode) {
ZFPlayerScalingModeNone,
ZFPlayerScalingModeAspectFit,
ZFPlayerScalingModeAspectFill,
ZFPlayerScalingModeFill
};
- 协议属性:
/// 播放器视图继承于ZFPlayerView,处理一些手势冲突
@property (nonatomic) ZFPlayerView *view;
/// 0...1.0,播放器音量,不影响设备的音量大小
@property (nonatomic) float volume;
/// 播放器是否静音,不影响设备静音
@property (nonatomic, getter=isMuted) BOOL muted;
/// 0.5...2,播放速率,正常速率为 1
@property (nonatomic) float rate;
/// 当前播放时间
@property (nonatomic, readonly) NSTimeInterval currentTime;
/// 播放总时间
@property (nonatomic, readonly) NSTimeInterval totalTime;
/// 缓冲时间
@property (nonatomic, readonly) NSTimeInterval bufferTime;
/// 视频播放定位时间
@property (nonatomic) NSTimeInterval seekTime;
/// 视频是否正在播放中
@property (nonatomic, readonly) BOOL isPlaying;
/// 视频播放视图的填充模式,默认不做任何拉伸
@property (nonatomic) ZFPlayerScalingMode scalingMode;
/// 检查视频播放是否准备就绪,返回YES,调用play方法直接播放视频;返回NO,调用play方法内部自动调用prepareToPlay方法进行视频播放准备工作
@property (nonatomic, readonly) BOOL isPreparedToPlay;
/// 媒体播放资源URL
@property (nonatomic) NSURL *assetURL;
/// 视频的尺寸
@property (nonatomic, readonly) CGSize presentationSize;
/// 视频播放状态
@property (nonatomic, readonly) ZFPlayerPlaybackState playState;
/// 视频的加载状态
@property (nonatomic, readonly) ZFPlayerLoadState loadState;
/// 准备播放
@property (nonatomic, copy, nullable) void(^playerPrepareToPlay)(id<ZFPlayerMediaPlayback> asset, NSURL *assetURL);
/// 播放进度改变
@property (nonatomic, copy, nullable) void(^playerPlayTimeChanged)(id<ZFPlayerMediaPlayback> asset, NSTimeInterval currentTime, NSTimeInterval duration);
/// 视频缓冲进度改变
@property (nonatomic, copy, nullable) void(^playerBufferTimeChanged)(id<ZFPlayerMediaPlayback> asset, NSTimeInterval bufferTime);
/// 视频播放状态改变
@property (nonatomic, copy, nullable) void(^playerPlayStatChanged)(id<ZFPlayerMediaPlayback> asset, ZFPlayerPlaybackState playState);
/// 视频加载状态改变
@property (nonatomic, copy, nullable) void(^playerLoadStatChanged)(id<ZFPlayerMediaPlayback> asset, ZFPlayerLoadState loadState);
/// 视频播放已经结束
@property (nonatomic, copy, nullable) void(^playerDidToEnd)(id<ZFPlayerMediaPlayback> asset);
- 协议方法:
/// 视频播放准备,中断除non-mixible之外的任何音频会话
- (void)prepareToPlay;
/// 重新进行视频播放准备
- (void)reloadPlayer;
/// 视频播放
- (void)play;
/// 视频暂停
- (void)pause;
/// 视频重新播放
- (void)replay;
/// 视频播放停止
- (void)stop;
/// 视频播放当前时间的画面截图
- (UIImage *)thumbnailImageAtCurrentTime;
/// 替换当前媒体资源地址
- (void)replaceCurrentAssetURL:(NSURL *)assetURL;
/// 调节播放进度
- (void)seekToTime:(NSTimeInterval)time completionHandler:(void (^ __nullable)(BOOL finished))completionHandler;
ZFPlayerMediaControl—控制层遵守的协议
- 视频状态相关
/// 视频播放准备就绪
- (void)videoPlayer:(ZFPlayerController *)videoPlayer prepareToPlay:(NSURL *)assetURL;
/// 视频播放状态改变
- (void)videoPlayer:(ZFPlayerController *)videoPlayer playStateChanged:(ZFPlayerPlaybackState)state;
/// 视频加载状态改变
- (void)videoPlayer:(ZFPlayerController *)videoPlayer loadStateChanged:(ZFPlayerLoadState)state;
- 播放进度
/// 视频播放时间进度
- (void)videoPlayer:(ZFPlayerController *)videoPlayer
currentTime:(NSTimeInterval)currentTime
totalTime:(NSTimeInterval)totalTime;
/// 视频缓冲进度
- (void)videoPlayer:(ZFPlayerController *)videoPlayer
bufferTime:(NSTimeInterval)bufferTime;
/// 视频定位播放时间
- (void)videoPlayer:(ZFPlayerController *)videoPlayer
draggingTime:(NSTimeInterval)seekTime
totalTime:(NSTimeInterval)totalTime;
/// 视频播放结束
- (void)videoPlayerPlayEnd:(ZFPlayerController *)videoPlayer;
- 锁屏
/// 设置播放器锁屏时的协议方法
- (void)lockedVideoPlayer:(ZFPlayerController *)videoPlayer lockedScreen:(BOOL)locked;
- 屏幕旋转
/// 播放器全屏模式即将改变
- (void)videoPlayer:(ZFPlayerController *)videoPlayer orientationWillChange:(ZFOrientationObserver *)observer;
/// 播放器全屏模式已经改变
- (void)videoPlayer:(ZFPlayerController *)videoPlayer orientationDidChanged:(ZFOrientationObserver *)observer;
/// 当前网络状态发生变化
- (void)videoPlayer:(ZFPlayerController *)videoPlayer reachabilityChanged:(ZFReachabilityStatus)status;
- 手势方法
/// 相关手势设置
- (BOOL)gestureTriggerCondition:(ZFPlayerGestureControl *)gestureControl
gestureType:(ZFPlayerGestureType)gestureType
gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
touch:(UITouch *)touch;
/// 单击
- (void)gestureSingleTapped:(ZFPlayerGestureControl *)gestureControl;
/// 双击
- (void)gestureDoubleTapped:(ZFPlayerGestureControl *)gestureControl;
/// 开始拖拽
- (void)gestureBeganPan:(ZFPlayerGestureControl *)gestureControl
panDirection:(ZFPanDirection)direction
panLocation:(ZFPanLocation)location;
/// 拖拽中
- (void)gestureChangedPan:(ZFPlayerGestureControl *)gestureControl
panDirection:(ZFPanDirection)direction
panLocation:(ZFPanLocation)location
withVelocity:(CGPoint)velocity;
/// 拖拽结束
- (void)gestureEndedPan:(ZFPlayerGestureControl *)gestureControl
panDirection:(ZFPanDirection)direction
panLocation:(ZFPanLocation)location;
/// 捏合手势变化
- (void)gesturePinched:(ZFPlayerGestureControl *)gestureControl
scale:(float)scale;
- scrollView上的播放器视图方法
/// scrollView中的播放器视图已经出现(包括tableView或collectionView)
- (void)playerDidAppearInScrollView:(ZFPlayerController *)videoPlayer;
/// scrollView中的播放器视图即将消失(包括tableView或collectionView)
- (void)playerWillDisappearInScrollView:(ZFPlayerController *)videoPlayer;
/// scrollView中的播放器视图消失过半(包括tableView或collectionView)
- (void)playerDisappearHalfInScrollView:(ZFPlayerController *)videoPlayer;
/// scrollView中的播放器视图已经消失(包括tableView或collectionView)
- (void)playerDidDisappearInScrollView:(ZFPlayerController *)videoPlayer;
UIScrollView+ZFPlayer (处理列表播放相关逻辑)
列表列表播放区域如上图所示,主要代码在 scrollViewDidScroll
方法中,
主要思想就是把播放器View的frame转到列表的父视图上,topSpacing和bottomSpacing分别是,播放器上边线、下边线与列表父视图上边线、下边线的距离。根据不同的滚动方向来比较topSpacing和bottomSpacing值,来判断当前播放的view是否在可视区域。
当手向上滑动(滑动看列表下边内容)时候,当(topSpacing <= 0 && topSpacing > -播放器view高度的一半)播放器View即将消失,当(topSpacing <= -播放器view高度的一半 && topSpacing > -播放器view高度)播放器View已经消失一半了,当(topSpacing <= -播放器view高度))播放器view已经完全消失,当(topSpacing > 0 && topSpacing < 列表父视图高度)此时播放器View在可视区域内....接下来相关逻辑不写了,具体看代码吧,注释写的很详细。
代码传送门:https://github.com/renzifeng/ZFPlayer
未完待续...
Author
- Weibo: @任子丰
- Email: zifeng1300@gmail.com
- QQ Group: 213375947(付费群,提供技术支持)
打赏作者
你的支持就是我的动力,请作者喝杯下午茶吧!