SJVideoPlayer
基于AVPlayer.
极速初始化, 不阻塞主线程.
这个应该是目前基于AVPlayer的播放器中, 功能最全的一个吧.
_
使用
pod 'SJVideoPlayer'
_
功能
- 支持 全屏手势返回
- 支持 网络状态变更提示
- 支持 随心所欲的控制旋转
- 支持 导出片段 + 截屏 + 生成GIF
- 支持 自定义控制层(实现协议方法即可)
- 支持 显示提示文本(如: 现有的网路状态切换提示)
- 支持 调整亮度/声音/调速/静音/捏合, 播放 本地/网络视频
- 支持 在TableView的HeaderView或者Cell上播放, 在CollectionView的Cell中播放
- 💖 续播, 跳入下一个界面可以使用上一个界面的资源初始化
- 本地化处理. 支持简体/繁体/英文
- 可以 在后台播放视频
- 💖 无缝切换您自定义的控制层
_
全屏手势返回
只需pod到项目中即可带全屏手势
为啥自定义手势?
在使用原生手势返回时, 当前播放的视频会出现卡帧的问题. 原因我不太清楚. 我查看了腾讯视频和爱奇艺等App均为自己实现的手势返回.
当时使用了一个全屏返回的三方库. 是截屏返回的, 发现存在两个问题: 一是截屏耗时, 二是视频部分截取不了(黑色的)
由于我们起步8.0, 我便使用了新的APIsnapshotViewAfterScreenUpdates:
(这个接口其实7.0就有了)来替换截屏的方式, 直接使用视图快照, 这个接口一举两得, 不仅速度快, 而且视频播放也可以截取到.
但是快照是一个view, 并不是image, 原来的三方库截屏方式无法使用, 于是我重新撸了一个全屏手势.
手势使用
pod 'SJFullscreenPopGesture'
手势功能介绍
// default is `SJFullscreenPopGestureType_EdgeLeft`.
typedef NS_ENUM(NSUInteger, SJFullscreenPopGestureType) {
SJFullscreenPopGestureType_EdgeLeft, // 默认, 屏幕左边缘触发手势
SJFullscreenPopGestureType_Full, // 全屏触发手势
};
目前有两种:
- [类似腾讯视频返回.gif](https://upload-images.jianshu.io/upload_images/2318691-d5a992c40cfee5bb.gif?imageMogr2/auto-orient/strip)
- [在1的基础上加上了一层遮罩.gif](https://upload-images.jianshu.io/upload_images/2318691-3dcd02f47b0dff4a.gif?imageMogr2/auto-orient/strip)
/// 将要拖拽
@property (nonatomic, copy, readwrite, nullable) void(^sj_viewWillBeginDragging)(__kindof UIViewController *vc);
/// 拖拽中
@property (nonatomic, copy, readwrite, nullable) void(^sj_viewDidDrag)(__kindof UIViewController *vc);
/// 结束拖拽
@property (nonatomic, copy, readwrite, nullable) void(^sj_viewDidEndDragging)(__kindof UIViewController *vc);
等 ...
_
网络状态切换提示
网络状态提示就是当网络状态变更时, 播放器显示一行字或者图片等, 提示用户网络变更了.
对于默认提示的内容(可自定义), 我做了本地化处理, 根据 localLanguage 自动选择一种语言提示(中文/繁体/英文), 如下:
随心所欲的控制旋转
关于旋转我想吐槽一下: 大部分三方库的旋转是直接把播放器添加到window的中心, 再做一个transform动画.. .
旋转过程中, 界面会闪一下, 这个体验几次眼就会累... 为了解决这个问题, 我又写了一个旋转的库在添加之前, 我做了相应的坐标转换, 使播放器添加到window上时, 还处于原始位置. 然后去做transform动画并更新bounds和center.
播放器除了自动旋转之外, 您可以随意的控制旋转. 支持的方向如下:
/// 竖屏 || (左右)横屏
/// Auto rotate supported orientation
typedef NS_ENUM(NSUInteger, SJAutoRotateSupportedOrientation) {
SJAutoRotateSupportedOrientation_All,
SJAutoRotateSupportedOrientation_Portrait = 1 << 0,
SJAutoRotateSupportedOrientation_LandscapeLeft = 1 << 1, // UIDeviceOrientationLandscapeLeft
SJAutoRotateSupportedOrientation_LandscapeRight = 1 << 2, // UIDeviceOrientationLandscapeRight
};
关于旋转的一些方法:
/**
Autorotation. Animated.
*/
- (void)rotate;
/**
Rotate to the specified orientation.
@param orientation Any value of SJOrientation.
@param animated Whether or not animation.
*/
- (void)rotate:(SJOrientation)orientation animated:(BOOL)animated;
/**
Rotate to the specified orientation.
@param orientation Any value of SJOrientation.
@param animated Whether or not animation.
@param block The block invoked when player rotated.
*/
- (void)rotate:(SJOrientation)orientation animated:(BOOL)animated completion:(void (^ _Nullable)(__kindof SJBaseVideoPlayer *player))block;
旋转的一些设置
/**
The block invoked When player will rotate.
readwrite.
*/
@property (nonatomic, copy, nullable) void(^willRotateScreen)(__kindof SJBaseVideoPlayer *player, BOOL isFullScreen);
/**
The block invoked when player rotated.
readwrite.
*/
@property (nonatomic, copy, nullable) void(^rotatedScreen)(__kindof SJBaseVideoPlayer *player, BOOL isFullScreen);
导出片段 + 截屏 + 生成GIF
这几个功能都已封装在默认的控制层中, 并且相应的提示文本也做了本地化处理
方法如下:
- (UIImage * __nullable)screenshot;
- (void)screenshotWithTime:(NSTimeInterval)time
completion:(void(^)(__kindof SJBaseVideoPlayer *videoPlayer, UIImage * __nullable image, NSError *__nullable error))block;
- (void)screenshotWithTime:(NSTimeInterval)time
size:(CGSize)size
completion:(void(^)(__kindof SJBaseVideoPlayer *videoPlayer, UIImage * __nullable image, NSError *__nullable error))block;
/**
export session.
@param beginTime unit is sec.
@param endTime unit is sec.
@param presetName default is `AVAssetExportPresetMediumQuality`.
@param progressBlock progressBlock
@param completion completion
@param failure failure
*/
- (void)exportWithBeginTime:(NSTimeInterval)beginTime
endTime:(NSTimeInterval)endTime
presetName:(nullable NSString *)presetName
progress:(void(^)(__kindof SJBaseVideoPlayer *videoPlayer, float progress))progressBlock
completion:(void(^)(__kindof SJBaseVideoPlayer *videoPlayer, NSURL *fileURL, UIImage *thumbnailImage))completion
failure:(void(^)(__kindof SJBaseVideoPlayer *videoPlayer, NSError *error))failure;
- (void)cancelExportOperation;
- (void)generateGIFWithBeginTime:(NSTimeInterval)beginTime
duration:(NSTimeInterval)duration
progress:(void(^)(__kindof SJBaseVideoPlayer *videoPlayer, float progress))progressBlock
completion:(void(^)(__kindof SJBaseVideoPlayer *videoPlayer, UIImage *imageGIF, UIImage *thumbnailImage, NSURL *filePath))completion
failure:(void(^)(__kindof SJBaseVideoPlayer *videoPlayer, NSError *error))failure;
- (void)cancelGenerateGIFOperation;
_
显示提示文本
目前提示文本支持 NSString(可自定义字体) 以及 NSAttributedString. 正如前面看到的网路状态提示, 就是使用的这些方法, 方法如下:
/**
The middle of the player view shows the specified title. duration default is 1.0.
@param title prompt.
*/
- (void)showTitle:(NSString *)title;
/**
The middle of the view shows the specified title.
@param title prompt.
@param duration prompt duration. duration if value set -1, prompt will always show.
*/
- (void)showTitle:(NSString *)title duration:(NSTimeInterval)duration;
- (void)showTitle:(NSString *)title duration:(NSTimeInterval)duration hiddenExeBlock:(void(^__nullable)(__kindof SJBaseVideoPlayer *player))hiddenExeBlock;
- (void)showAttributedString:(NSAttributedString *)attributedString duration:(NSTimeInterval)duration;
- (void)showAttributedString:(NSAttributedString *)attributedString duration:(NSTimeInterval)duration hiddenExeBlock:(void(^__nullable)(__kindof SJBaseVideoPlayer *player))hiddenExeBlock;
/**
Hidden Prompt.
*/
- (void)hiddenTitle;
_
播放器的手势介绍
-
默认会存在四种手势: Single Tap, double Tap, Pan, Pinch.
- SingleTap
单击手势
当用户单击播放器时, 播放器会调用显示或隐藏控制层的相关代理方法. 见 controlLayerDelegate
- DoubleTap
双击手势
双击会触发暂停或播放的操作 - Pan
移动手势
当用户水平滑动时, 会触发控制层相应的代理方法. 见controlLayerDelegate
当用户垂直滑动时, 如果在屏幕左边, 则会触发调整亮度的操作, 并显示亮度提示视图. 如果在屏幕右边, 则会触发调整声音的操作, 并显示系统音量提示视图 - Pinch
捏合手势
当用户做放大或收缩触发该手势时, 会设置播放器显示模式Aspect
或AspectFill
.
_
关于自定义控制层的相关协议
下面介绍如何自定义一个控制层, 相关的方法后面, 我都会跟上一个相应实现.
数据源介绍
@protocol SJVideoPlayerControlLayerDataSource <NSObject>
@required
- (UIView *)controlView;
- (BOOL)controlLayerDisappearCondition;
- (BOOL)triggerGesturesCondition:(CGPoint)location;
@optional
- (void)installedControlViewToVideoPlayer:(__kindof SJBaseVideoPlayer *)videoPlayer;
@end
-
- (UIView *)controlView;
请返回控制层的根视图, 这个视图将会添加的播放器视图中. -
- (BOOL)controlLayerDisappearCondition;
当控制层显示时, 播放器会在一段时间(默认3秒)后尝试隐藏控制层, 此时会调用该方法, 如果返回YES, 则隐藏控制层, 否之. -
- (BOOL)triggerGesturesCondition:(CGPoint)location;
播放器中的手势 触发手势之前会调用这个方法, 如果返回NO, 将不会触发手势.
代理介绍
控制层的显示与隐藏
@required
/**Objective-C
This method will be called when the control layer needs to be appear. You should do some appear work here.
*/
- (void)controlLayerNeedAppear:(__kindof SJBaseVideoPlayer *)videoPlayer;
/**
This method will be called when the control layer needs to be disappear. You should do some disappear work here.
*/
- (void)controlLayerNeedDisappear:(__kindof SJBaseVideoPlayer *)videoPlayer;
播放器会接管控制层的显示与隐藏, 也就是当SingleTap时播放器会调用controlLayerNeedAppear:
播放器会在一段时间后调用隐藏controlLayerNeedDisappear:
.
-
controlLayerNeedAppear:
控制层需要显示, 代理可以在这个方法里面实现显示控制层的UI -
controlLayerNeedDisappear:
控制层需要隐藏, 同上, 做一些隐藏工作
在UITableView或者CollectionView中播放时的代理回调
- (void)videoPlayerWillAppearInScrollView:(__kindof SJBaseVideoPlayer *)videoPlayer;
- (void)videoPlayerWillDisappearInScrollView:(__kindof SJBaseVideoPlayer *)videoPlayer;
-
videoPlayerWillAppearInScrollView:
播放器在滚动视图中将要显示时, 会调用它 -
videoPlayerWillDisappearInScrollView:
播放器在滚动视图中将要消失时, 会调用它
准备播放一个资源时以及播放状态/播放进度的回调
- (void)videoPlayer:(__kindof SJBaseVideoPlayer *)videoPlayer prepareToPlay:(SJVideoPlayerURLAsset *)asset;
- (void)videoPlayer:(__kindof SJBaseVideoPlayer *)videoPlayer stateChanged:(SJVideoPlayerPlayState)state;
- (void)videoPlayer:(__kindof SJBaseVideoPlayer *)videoPlayer
currentTime:(NSTimeInterval)currentTime currentTimeStr:(NSString *)currentTimeStr
totalTime:(NSTimeInterval)totalTime totalTimeStr:(NSString *)totalTimeStr;
-
videoPlayer:prepareToPlay:
当播放器播放一个新的资源时, 会调用这个方法 -
videoPlayer:stateChanged:
当播放状态变更时, 会调用这个方法 -
第三个方法
播放时的回调, 当前时间及视频的全部时长. 单位都是秒
缓存的状态回调
- (void)videoPlayer:(__kindof SJBaseVideoPlayer *)videoPlayer loadedTimeProgress:(float)progress;
- (void)startLoading:(__kindof SJBaseVideoPlayer *)videoPlayer;
- (void)cancelLoading:(__kindof SJBaseVideoPlayer *)videoPlayer;
- (void)loadCompletion:(__kindof SJBaseVideoPlayer *)videoPlayer;
-
videoPlayer:loadedTimeProgress:
缓冲进度的回调 -
startLoading:
开始缓冲的回调 -
cancelLoading:
取消缓冲的回调 -
loadCompletion:
缓冲完成的回调
锁屏的回调以及一个只有在锁屏状态下才触发的单击手势
- (void)lockedVideoPlayer:(__kindof SJBaseVideoPlayer *)videoPlayer;
- (void)unlockedVideoPlayer:(__kindof SJBaseVideoPlayer *)videoPlayer;
- (void)tappedPlayerOnTheLockedState:(__kindof SJBaseVideoPlayer *)videoPlayer;
-
lockedVideoPlayer:
当设置videoPlayer.lockedScreen = YES;
时, 会回调这个方法 -
unlockedVideoPlayer:
当设置videoPlayer.lockedScreen = NO;
时, 会回调这个方法 -
tappedPlayerOnTheLockedState:
当在锁屏状态下时, 单击播放器, 会回调这个方法
屏幕旋转的回调
- (void)videoPlayer:(__kindof SJBaseVideoPlayer *)videoPlayer willRotateView:(BOOL)isFull;
- (void)videoPlayer:(__kindof SJBaseVideoPlayer *)videoPlayer didEndRotation:(BOOL)isFull;
-
videoPlayer:willRotateView:
当播放器 将要旋转时 会回调这个方法 -
videoPlayer:didEndRotation:
当播放器 旋转完成时 会回调这个方法
静音 / 音量 / 亮度 / 调速的回调
- (void)videoPlayer:(__kindof SJBaseVideoPlayer *)videoPlayer muteChanged:(BOOL)mute;
- (void)videoPlayer:(__kindof SJBaseVideoPlayer *)videoPlayer volumeChanged:(float)volume;
- (void)videoPlayer:(__kindof SJBaseVideoPlayer *)videoPlayer brightnessChanged:(float)brightness;
- (void)videoPlayer:(__kindof SJBaseVideoPlayer *)videoPlayer rateChanged:(float)rate;
-
videoPlayer:muteChanged:
设置静音时的回调 -
videoPlayer:volumeChanged:
声音改变的回调 -
videoPlayer:brightnessChanged:
亮度改变的回调 -
videoPlayer:rateChanged:
速率改变的回调
水平方向手势触发的回调
/// 水平方向开始拖动.
- (void)horizontalDirectionWillBeginDragging:(__kindof SJBaseVideoPlayer *)videoPlayer;
/**
@param progress drag progress
*/
- (void)videoPlayer:(__kindof SJBaseVideoPlayer *)videoPlayer horizontalDirectionDidMove:(CGFloat)progress;
/// 水平方向拖动结束.
- (void)horizontalDirectionDidEndDragging:(__kindof SJBaseVideoPlayer *)videoPlayer;
-
horizontalDirectionWillBeginDragging:
水平方向开始拖动时的回调 -
videoPlayer:horizontalDirectionDidMove:
水平方向拖动的进度的回调 -
horizontalDirectionDidEndDragging:
水平方向拖动结束的回调
Network
/// 网络状态变更
- (void)videoPlayer:(__kindof SJBaseVideoPlayer *)videoPlayer reachabilityChanged:(SJNetworkStatus)status;
-
videoPlayer:reachabilityChanged:
当网络状态变更时, 会回调这个方法
Demo
项目地址: https://github.com/changsanji...
我的邮箱: changsanjiang@gmail.com
如果您有什么建议, 望请联系我!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。