首先看下效果图
图做的不太好,将就看吧
前言
上一篇文章仿写了抖音的左右滑动效果-iOS之仿抖音左右滑动效果,有兴趣的可以去GKNavigationBarViewController的demo中查看。
这篇文章主要是对抖音的上下滑动及视频播放功能做介绍。其中上下滑动用的是UIScrollview,包含3个子视图。视频播放用的是TX的独立播放器TXLiteAVSDK_Player,可实现音视频的点播、直播等功能。
demo中的视频是通过抓包获取的百度的伙拍小视频,仅供学习使用。
说明
1、上下滑动切换视图实现
主要是在UIScrollview的代理中做处理,且对滑动到第一个和最后一个时分开处理,看下代码
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// 小于等于三个,不用处理
if (self.videos.count <= 3) return;
// 上滑到第一个
if (self.index == 0 && scrollView.contentOffset.y <= SCREEN_HEIGHT) {
return;
}
// 下滑到最后一个
if (self.index == self.videos.count - 1 && scrollView.contentOffset.y > SCREEN_HEIGHT) {
return;
}
// 判断是从中间视图上滑还是下滑
if (scrollView.contentOffset.y >= 2 * SCREEN_HEIGHT) { // 上滑
[self.player removeVideo]; // 在这里移除播放,解决闪动的bug
if (self.index == 0) {
self.index += 2;
scrollView.contentOffset = CGPointMake(0, SCREEN_HEIGHT);
self.topView.model = self.ctrView.model;
self.ctrView.model = self.btmView.model;
}else {
self.index += 1;
if (self.index == self.videos.count - 1) {
self.ctrView.model = self.videos[self.index - 1];
}else {
scrollView.contentOffset = CGPointMake(0, SCREEN_HEIGHT);
self.topView.model = self.ctrView.model;
self.ctrView.model = self.btmView.model;
}
}
if (self.index < self.videos.count - 1) {
self.btmView.model = self.videos[self.index + 1];
}
}else if (scrollView.contentOffset.y <= 0) { // 下滑
[self.player removeVideo]; // 在这里移除播放,解决闪动的bug
if (self.index == 1) {
self.topView.model = self.videos[self.index - 1];
self.ctrView.model = self.videos[self.index];
self.btmView.model = self.videos[self.index + 1];
self.index -= 1;
}else {
if (self.index == self.videos.count - 1) {
self.index -= 2;
}else {
self.index -= 1;
}
scrollView.contentOffset = CGPointMake(0, SCREEN_HEIGHT);
self.btmView.model = self.ctrView.model;
self.ctrView.model = self.topView.model;
if (self.index > 0) {
self.topView.model = self.videos[self.index - 1];
}
}
}
}
滑动结束后开始播放
// 结束滚动后开始播放
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if (scrollView.contentOffset.y == 0) {
if (self.currentPlayId == self.topView.model.post_id) return;
[self playVideoFrom:self.topView];
}else if (scrollView.contentOffset.y == SCREEN_HEIGHT) {
if (self.currentPlayId == self.ctrView.model.post_id) return;
[self playVideoFrom:self.ctrView];
}else if (scrollView.contentOffset.y == 2 * SCREEN_HEIGHT) {
if (self.currentPlayId == self.btmView.model.post_id) return;
[self playVideoFrom:self.btmView];
}
if (self.isPushed) return;
// 当只剩最后两个的时候,获取新数据
if (self.currentPlayIndex == self.videos.count - 2) {
[self.viewModel refreshNewListWithSuccess:^(NSArray * _Nonnull list) {
[self.videos addObjectsFromArray:list];
} failure:^(NSError * _Nonnull error) {
NSLog(@"%@", error);
}];
}
}
上下滑动的处理基本就这些,如果有不懂的可以下载demo查看。
2、播放器封装
这里封装的播放器用的是腾讯的视频点播TXVodPlayer。
创建播放器
- (TXVodPlayer *)player {
if (!_player) {
[TXLiveBase setLogLevel:LOGLEVEL_NULL];
[TXLiveBase setConsoleEnabled:NO];
_player = [TXVodPlayer new];
_player.vodDelegate = self;
_player.loop = YES; // 开启循环播放功能
}
return _player;
}
由于是多个视频切换播放,所以最好只用一个播放器,因此在切换视图后,需要播放器切换播放视图和播放地址,所以提供了下面的方法。
- (void)playVideoWithView:(UIView *)playView url:(NSString *)url {
// 设置播放视图
[self.player setupVideoWidget:playView insertIndex:0];
// 准备播放
[self playerStatusChanged:GKDYVideoPlayerStatusPrepared];
// 开始播放
if ([self.player startPlay:url] == 0) {
// 这里可加入缓冲视图
}else {
[self playerStatusChanged:GKDYVideoPlayerStatusError];
}
}
播放器状态监听,可获取播放状态及进度等
- (void)onPlayEvent:(TXVodPlayer *)player event:(int)EvtID withParam:(NSDictionary *)param {
switch (EvtID) {
case PLAY_EVT_PLAY_LOADING:{ // loading
if (self.status == GKDYVideoPlayerStatusPaused) {
[self playerStatusChanged:GKDYVideoPlayerStatusPaused];
}else {
[self playerStatusChanged:GKDYVideoPlayerStatusLoading];
}
}
break;
case PLAY_EVT_PLAY_BEGIN:{ // 开始播放
[self playerStatusChanged:GKDYVideoPlayerStatusPlaying];
}
break;
case PLAY_EVT_PLAY_END:{ // 播放结束
[self playerStatusChanged:GKDYVideoPlayerStatusEnded];
}
break;
case PLAY_ERR_NET_DISCONNECT:{ // 失败,多次重连无效
[self playerStatusChanged:GKDYVideoPlayerStatusError];
}
break;
case PLAY_EVT_PLAY_PROGRESS:{ // 进度
if (self.status == GKDYVideoPlayerStatusPlaying) {
self.duration = [param[EVT_PLAY_DURATION] floatValue];
float currTime = [param[EVT_PLAY_PROGRESS] floatValue];
float progress = self.duration == 0 ? 0 : currTime / self.duration;
// 处理播放结束时,进度不更新问题
if (progress >= 0.95) progress = 1.0f;
// float buffTime = [param[EVT_PLAYABLE_DURATION] floatValue];
// float burrProgress = self.duration == 0 ? 0 : buffTime / self.duration;
if ([self.delegate respondsToSelector:@selector(player:currentTime:totalTime:progress:)]) {
[self.delegate player:self currentTime:currTime totalTime:self.duration progress:progress];
}
}
}
break;
default:
break;
}
}
内容比较多,如果想要具体了解,还需要下载代码查看。
最后
demo的地址:GKDYVideo,由于github的限制,播放器无法上传,所以需要下载下来后pod install即可。
网友评论
2018-10-09 18:10:51.004412+0800 GKDYVideo[3494:1238270] Task <2A3E0886-B622-4D17-9245-98595AE677AC>.<1> finished with error - code: -1009
2018-10-09 18:10:51.075764+0800 GKDYVideo[3494:1238027] Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={NSUnderlyingError=0x1c40526f0 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={_kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}}, NSErrorFailingURLStringKey=http://c.tieba.baidu.com/c/f/nani/recommend/list?_client_type=1&;_client_version=2.2.0&_os_version=12.0&_phone_imei=9A910F65D70DBAE95866E00B75934C78%7Ccom.baidu.nani&_phone_newimei=9A910F65D70DBAE95866E00B75934C78%7Ccom.baidu.nani&_timestamp=1537782764313&brand=iPad&brand_type=Unknown%20iPad&cuid=9A910F65D70DBAE95866E00B75934C78%7Ccom.baidu.nani&diuc=C2D95DB95D613410309F81193FB324F01F9B14E32FHFSIKTGGF&dl=505F80E58F3817291B7768CE59A90AF8&from=AppStore&model=Unknown%20iPad&nani_idfa=86294854-68D7-49CD-