diff --git a/.DS_Store b/.DS_Store
index f4aa91cd2..c518eaa97 100644
Binary files a/.DS_Store and b/.DS_Store differ
diff --git a/Example/.DS_Store b/Example/.DS_Store
index a0299b08b..6bacc43a6 100644
Binary files a/Example/.DS_Store and b/Example/.DS_Store differ
diff --git a/Example/Pods/Pods.xcodeproj/xcuserdata/chenzhengzheng.xcuserdatad/xcschemes/xcschememanagement.plist b/Example/Pods/Pods.xcodeproj/xcuserdata/chenzhengzheng.xcuserdatad/xcschemes/xcschememanagement.plist
new file mode 100644
index 000000000..d32c1baf4
--- /dev/null
+++ b/Example/Pods/Pods.xcodeproj/xcuserdata/chenzhengzheng.xcuserdatad/xcschemes/xcschememanagement.plist
@@ -0,0 +1,69 @@
+
+
+
+
+ SchemeUserState
+
+ CocoaAsyncSocket.xcscheme_^#shared#^_
+
+ orderHint
+ 5
+
+ MJRefresh.xcscheme_^#shared#^_
+
+ orderHint
+ 6
+
+ Masonry.xcscheme_^#shared#^_
+
+ orderHint
+ 12
+
+ Pods-SJVideoPlayer_Example.xcscheme_^#shared#^_
+
+ orderHint
+ 4
+
+ SDWebImage.xcscheme_^#shared#^_
+
+ orderHint
+ 3
+
+ SJBaseVideoPlayer.xcscheme_^#shared#^_
+
+ orderHint
+ 11
+
+ SJFullscreenPopGesture.xcscheme_^#shared#^_
+
+ orderHint
+ 9
+
+ SJMediaCacheServer.xcscheme_^#shared#^_
+
+ orderHint
+ 8
+
+ SJRouter.xcscheme_^#shared#^_
+
+ orderHint
+ 2
+
+ SJUIKit.xcscheme_^#shared#^_
+
+ orderHint
+ 1
+
+ SJVideoPlayer.xcscheme_^#shared#^_
+
+ orderHint
+ 10
+
+ YYModel.xcscheme_^#shared#^_
+
+ orderHint
+ 7
+
+
+
+
diff --git a/Example/Pods/Pods.xcodeproj/xcuserdata/mac.xcuserdatad/xcschemes/xcschememanagement.plist b/Example/Pods/Pods.xcodeproj/xcuserdata/mac.xcuserdatad/xcschemes/xcschememanagement.plist
new file mode 100644
index 000000000..576d1a48e
--- /dev/null
+++ b/Example/Pods/Pods.xcodeproj/xcuserdata/mac.xcuserdatad/xcschemes/xcschememanagement.plist
@@ -0,0 +1,69 @@
+
+
+
+
+ SchemeUserState
+
+ CocoaAsyncSocket.xcscheme_^#shared#^_
+
+ orderHint
+ 6
+
+ MJRefresh.xcscheme_^#shared#^_
+
+ orderHint
+ 3
+
+ Masonry.xcscheme_^#shared#^_
+
+ orderHint
+ 2
+
+ Pods-SJVideoPlayer_Example.xcscheme_^#shared#^_
+
+ orderHint
+ 5
+
+ SDWebImage.xcscheme_^#shared#^_
+
+ orderHint
+ 4
+
+ SJBaseVideoPlayer.xcscheme_^#shared#^_
+
+ orderHint
+ 7
+
+ SJFullscreenPopGesture.xcscheme_^#shared#^_
+
+ orderHint
+ 10
+
+ SJMediaCacheServer.xcscheme_^#shared#^_
+
+ orderHint
+ 11
+
+ SJRouter.xcscheme_^#shared#^_
+
+ orderHint
+ 12
+
+ SJUIKit.xcscheme_^#shared#^_
+
+ orderHint
+ 1
+
+ SJVideoPlayer.xcscheme_^#shared#^_
+
+ orderHint
+ 9
+
+ YYModel.xcscheme_^#shared#^_
+
+ orderHint
+ 8
+
+
+
+
diff --git a/Example/SJVideoPlayer.xcodeproj/xcuserdata/chenzhengzheng.xcuserdatad/xcschemes/xcschememanagement.plist b/Example/SJVideoPlayer.xcodeproj/xcuserdata/chenzhengzheng.xcuserdatad/xcschemes/xcschememanagement.plist
new file mode 100644
index 000000000..8a3192747
--- /dev/null
+++ b/Example/SJVideoPlayer.xcodeproj/xcuserdata/chenzhengzheng.xcuserdatad/xcschemes/xcschememanagement.plist
@@ -0,0 +1,14 @@
+
+
+
+
+ SchemeUserState
+
+ SJVideoPlayer-Example.xcscheme_^#shared#^_
+
+ orderHint
+ 0
+
+
+
+
diff --git a/Example/SJVideoPlayer.xcodeproj/xcuserdata/mac.xcuserdatad/xcschemes/xcschememanagement.plist b/Example/SJVideoPlayer.xcodeproj/xcuserdata/mac.xcuserdatad/xcschemes/xcschememanagement.plist
new file mode 100644
index 000000000..8a3192747
--- /dev/null
+++ b/Example/SJVideoPlayer.xcodeproj/xcuserdata/mac.xcuserdatad/xcschemes/xcschememanagement.plist
@@ -0,0 +1,14 @@
+
+
+
+
+ SchemeUserState
+
+ SJVideoPlayer-Example.xcscheme_^#shared#^_
+
+ orderHint
+ 0
+
+
+
+
diff --git a/Example/SJVideoPlayer.xcworkspace/xcuserdata/chenzhengzheng.xcuserdatad/UserInterfaceState.xcuserstate b/Example/SJVideoPlayer.xcworkspace/xcuserdata/chenzhengzheng.xcuserdatad/UserInterfaceState.xcuserstate
new file mode 100644
index 000000000..b3babeaa9
Binary files /dev/null and b/Example/SJVideoPlayer.xcworkspace/xcuserdata/chenzhengzheng.xcuserdatad/UserInterfaceState.xcuserstate differ
diff --git a/Example/SJVideoPlayer.xcworkspace/xcuserdata/mac.xcuserdatad/UserInterfaceState.xcuserstate b/Example/SJVideoPlayer.xcworkspace/xcuserdata/mac.xcuserdatad/UserInterfaceState.xcuserstate
new file mode 100644
index 000000000..e8e97c6cc
Binary files /dev/null and b/Example/SJVideoPlayer.xcworkspace/xcuserdata/mac.xcuserdatad/UserInterfaceState.xcuserstate differ
diff --git a/Example/SJVideoPlayer/Common/Model/SJVideoModel.h b/Example/SJVideoPlayer/Common/Model/SJVideoModel.h
index 518d3ff2a..950905562 100644
--- a/Example/SJVideoPlayer/Common/Model/SJVideoModel.h
+++ b/Example/SJVideoPlayer/Common/Model/SJVideoModel.h
@@ -21,6 +21,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, copy, nullable) NSString *avatar;
@property (nonatomic, copy, nullable) NSString *username;
@property (nonatomic, strong, nullable) NSURL *URL;
+@property (nonatomic) NSError *error;
@end
NS_ASSUME_NONNULL_END
diff --git a/Example/SJVideoPlayer/Demos/DY/List/SJDYPlaybackListViewController.m b/Example/SJVideoPlayer/Demos/DY/List/SJDYPlaybackListViewController.m
index cf5260f62..3c1e6fe11 100644
--- a/Example/SJVideoPlayer/Demos/DY/List/SJDYPlaybackListViewController.m
+++ b/Example/SJVideoPlayer/Demos/DY/List/SJDYPlaybackListViewController.m
@@ -27,6 +27,10 @@ @interface SJDYPlaybackListViewController () prePrefetchTask;
@property (nonatomic, strong, nullable) id nextPrefetchTask;
+
+@property (nonatomic ,assign) BOOL isLoadingData; //是否加载正在数据中
+@property (nonatomic ,assign) BOOL isCacheData; //是否有缓存数据
+
@end
@implementation SJDYPlaybackListViewController
@@ -78,6 +82,12 @@ - (void)tableView:(UITableView *)tableView willDisplayCell:(SJDYTableViewCell *)
if ( _curPlayer == nil ) {
_curPlayer = cell.player;
}
+ //提前请求数据
+ if (self.list.count - indexPath.row <= 3 &&
+ !self.isLoadingData) {
+ self.isLoadingData = YES;
+ [self loadDataWithShowLoading:NO];
+ }
[_prePrefetchTask cancel];
[_nextPrefetchTask cancel];
_prePrefetchTask = [self _prefetchTaskWithIndex:indexPath.row - 1];
@@ -116,7 +126,7 @@ - (void)_checkVisibleCells {
max = intersection;
}
}
-
+
if ( target == nil )
return;
@@ -124,9 +134,9 @@ - (void)_checkVisibleCells {
if ( cur != _curPlayer ) {
[_curPlayer pause];
}
-
+
_curPlayer = cur;
-
+
if ( !_curPlayer.isUserPaused ) {
[_curPlayer play];
}
@@ -154,48 +164,112 @@ - (void)_setupTableView {
}];
[SJDYTableViewCell registerWithTableView:_tableView];
-
- // 请求列表数据
- // 模拟请求网络数据
+ [self loadDataWithShowLoading:YES];//首次请求数据
+ [_tableView sj_exeHeaderRefreshingAnimated:NO]; // 执行头部刷新
+}
+#pragma Mark 本次修改主要为这里,在请求刷新处做无感处理
+- (void)loadDataWithShowLoading:(BOOL)isShowLoading{
+ //当有缓存未加载时,直接展示缓存数据
+ if (self.isCacheData) {
+ [self refreshView];
+ return;
+ }
+ // 模拟请求网络数据
__weak typeof(self) _self = self;
- [_tableView sj_setupRefreshingWithPageSize:20 beginPageNum:1 refreshingBlock:^(__kindof UIScrollView * _Nonnull scrollView, NSInteger requestPageNum) {
- __strong typeof(_self) self = _self;
- if ( !self ) return;
-
+ // 提前获取数据时,无须显示loading状态,直接请求数据。
+ if (!isShowLoading) {
// 模拟延迟
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- [self.dataProvider playbackListWithPageNum:self.tableView.sj_pageNum pageSize:self.tableView.sj_pageSize completionHandler:^(NSArray * _Nullable list, NSError * _Nullable error) {
- __strong typeof(_self) self = _self;
- if ( !self ) return;
- if ( error ) {
- // error
- NSLog(@"%@", error);
- return;
- }
- // 如果是请求首页的数据, 即下拉
- if ( self.tableView.sj_pageNum == self.tableView.sj_beginPageNum ) {
- // 下拉刷新时, 清理播放数据
- [self.curPlayer stop];
- self.curPlayer = nil;
- [self.list removeAllObjects];
- }
+ [self.dataProvider playbackListWithPageNum:self.tableView.sj_pageNum pageSize:self.tableView.sj_pageSize completionHandler:^(NSArray * _Nullable list, NSError * _Nullable error) {
+
+ __strong typeof(_self) self = _self;
+
+ if ( !self ) return;
+
+ self.isLoadingData = NO;
+
+ if ( error ) {
+ // error
+ [self.tableView sj_endRefreshing];
+ NSLog(@"%@", error);
+ return;
+ }
+
+ if (list.count == 0){
+ [self.tableView sj_endRefreshing];
+ [self.tableView sj_endRefreshingWithItemCount:0];
+ return;
+ }
+
+ // 如果是请求首页的数据, 即下拉
+ if ( self.tableView.sj_pageNum == self.tableView.sj_beginPageNum ) {
+ // 下拉刷新时, 清理播放数据
+ [self.curPlayer stop];
+ self.curPlayer = nil;
+ [self.list removeAllObjects];
- // 添加到播放列表中, 刷新列表
+ }
+
+ NSIndexPath *idxP = [self.tableView indexPathsForVisibleRows].lastObject;
+ //如果当前是最后一个,或者下拉刷新,直接刷新页面
+ if (self.tableView.sj_pageNum == 1) {
[self.list addObjectsFromArray:list];
+ [self refreshView];
+ }else if (idxP &&
+ (self.list.count - 1 == idxP.row) &&
+ self.tableView.sj_footerRefreshState == SJWrapRefreshStateRefreshing
+ ) {
+
+ [self.list addObjectsFromArray:list];
+ self.isCacheData = NO;
+ [self.tableView sj_endRefreshingWithItemCount:self.list.count];
[self.tableView reloadData];
- [self.tableView sj_endRefreshingWithItemCount:list.count];
- [self _checkVisibleCells];
- }];
- });
- }];
-
- [_tableView sj_exeHeaderRefreshingAnimated:NO]; // 执行头部刷新
-}
+
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_global_queue(0, 0), ^{
+
+ dispatch_sync(dispatch_get_main_queue(), ^{
+ __strong typeof(self) strongSelf = _self;
+ [strongSelf.tableView setContentOffset:CGPointMake(strongSelf.tableView.contentOffset.x, strongSelf.tableView.contentOffset.y + strongSelf.tableView.frame.size.height) animated:YES];
+ });
+
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
+ __strong typeof(self) strongSelf = _self;
+ [strongSelf _checkVisibleCells];
+ });
+
+ });
+// 无数据判断
+// if (self.list.count == totalElements){
+// [self.tableView sj_endRefreshingWithItemCount:0];
+// }
+ }else{
+ [self.list addObjectsFromArray:list];
+ self.isCacheData = YES;
+ }
+ }];
+ }else{
+ //只展示loading状态,只有下拉刷新时主动获取数据 size 控制每次请求数据多少
+ [_tableView sj_setupRefreshingWithPageSize:5 beginPageNum:1 refreshingBlock:^(__kindof UIScrollView * _Nonnull scrollView, NSInteger requestPageNum) {
+ __strong typeof(_self) self = _self;
+ if ( !self ) return;
+
+ if (requestPageNum == 1 ||
+ !self.isLoadingData) {
+ [self loadDataWithShowLoading:NO];
+ }
+ }];
+ }
+}
+- (void)refreshView{
+ self.isCacheData = NO;
+ [self.tableView reloadData];
+ [self.tableView sj_endRefreshingWithItemCount:self.list.count];
+ [self _checkVisibleCells];
+}
// 配置边播边缓存模块
- (void)_setupMediaCacheServer {
// 开启控制台log
- SJMediaCacheServer.shared.enabledConsoleLog = YES;
+ SJMediaCacheServer.shared.enabledConsoleLog = NO;
SJMediaCacheServer.shared.resolveAssetIdentifier = ^NSString * _Nonnull(NSURL * _Nonnull URL) {
// 由于demo中同一个视频的URL后面的参数不一样, 为保证引用相同的缓存文件, 这里删除了URL后面的所有参数
// 例如:
@@ -206,12 +280,13 @@ - (void)_setupMediaCacheServer {
};
}
-#pragma mark -
+#pragma mark - 此处修改为请求数据回来直接批量缓存,因为现在改为无感刷新,所以每次可以请求少量数据进行缓存,并且添加了报错处理,但还需要改进
-// 预加载指定位置的某个资源`1M`的内容
+// 预加载指定位置的某个资源`1M`的内容 缓存失败回调处理。
- (nullable id)_prefetchTaskWithIndex:(NSInteger)index {
if ( index < 0 || index >= _list.count )
return nil;
return [SJMediaCacheServer.shared prefetchWithURL:_list[index].URL preloadSize:1 * 1024 * 1024];
}
+
@end
diff --git a/Example/SJVideoPlayer/Demos/DY/List/SJDYTableViewCell.h b/Example/SJVideoPlayer/Demos/DY/List/SJDYTableViewCell.h
index dde3abab4..3a25ed68f 100644
--- a/Example/SJVideoPlayer/Demos/DY/List/SJDYTableViewCell.h
+++ b/Example/SJVideoPlayer/Demos/DY/List/SJDYTableViewCell.h
@@ -12,6 +12,7 @@
NS_ASSUME_NONNULL_BEGIN
@interface SJDYTableViewCell : SJBaseTableViewCell
@property (nonatomic, strong, readonly) id player;
+@property (nonatomic) NSIndexPath *indexPath;
@end
@protocol SJDYDemoPlayer
diff --git a/Example/SJVideoPlayer/Demos/DY/List/SJDYTableViewCell.m b/Example/SJVideoPlayer/Demos/DY/List/SJDYTableViewCell.m
index d95e092f1..e81775ea4 100644
--- a/Example/SJVideoPlayer/Demos/DY/List/SJDYTableViewCell.m
+++ b/Example/SJVideoPlayer/Demos/DY/List/SJDYTableViewCell.m
@@ -97,7 +97,7 @@ - (instancetype)init {
if ( self ) {
self.view.backgroundColor = UIColor.clearColor;
self.presentView.backgroundColor = UIColor.clearColor;
- self.videoGravity = AVLayerVideoGravityResizeAspectFill;
+ self.videoGravity = AVLayerVideoGravityResizeAspect;
self.autoplayWhenSetNewAsset = NO;
self.rotationManager.disabledAutorotation = YES;
self.pauseWhenAppDidEnterBackground = YES;