类似搜狐视频app视频列表播放
2015-09-19 17:40
477 查看
有些视频app有一个视频列表播放功能,例如搜狐视频app的热点模块,腾讯视频app的热点模块等。进入此页面会自动播放视频,滑动页面还会自动切换播放视频,同时支持横竖屏切换,确实比较方便。研究了下,实现方法可以如下。
首先记录下tableview当前展示的所有cell的indexPath:
当页面停止滑动时要判断出需要播放的视频存在于第几个cell中,自动去播放其中的视频:
其次还要将此cell回传到ViewController中,在cell中添加delegate方法,注意如果之前有cell已经在播放视频,需要恢复初始状态
滑动列表停止时,如果之前播放的cell已经滚出屏幕,需要停止播放
然后为了实现横竖屏切换,添加监测屏幕方向变化的通知
实现对应的通知方法
最后要注意,离开页面时也要停止播放当前cell中的视频
OK,大功告成。
PS:项目完整版下载地址为:
https://github.com/wangqi211/VideoListPlayDemo
首先记录下tableview当前展示的所有cell的indexPath:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { [self addOneIndexPathToVisibleIndexArrayWithValue:indexPath]; } - (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { [_visibleIndexArray removeObject:indexPath]; }
当页面停止滑动时要判断出需要播放的视频存在于第几个cell中,自动去播放其中的视频:
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { self.currentPlayingIndex = [self getCurrentCellIndexShouldBePlaying]; }
其次还要将此cell回传到ViewController中,在cell中添加delegate方法,注意如果之前有cell已经在播放视频,需要恢复初始状态
- (void)videoPlayListTableViewCellPlayButtonTappedWithIndex:(NSInteger)index AndCell:(SCVideoPlayListTableViewCell *)cell { if(_currentPlayingCell){ [_currentPlayingCell backToInitState]; _currentPlayingCell = nil; } _currentPlayingIndex = index; _currentPlayingCell = cell; }
滑动列表停止时,如果之前播放的cell已经滚出屏幕,需要停止播放
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { [NSObject cancelPreviousPerformRequestsWithTarget:self]; BOOL isOutOfScreen = [self judgeCurrentFullScreenPlayingCellIfOutOfScreen]; if(isOutOfScreen){ [self rotateToPortrait]; if(_currentPlayingCell){ [_currentPlayingCell removeFromSuperview]; [_currentPlayingCell backToInitState]; _currentPlayingCell = nil; } } self.currentPlayingIndex = [self getCurrentCellIndexShouldBePlaying]; }
然后为了实现横竖屏切换,添加监测屏幕方向变化的通知
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationHasChange:) name:UIDeviceOrientationDidChangeNotification object:nil]; }
实现对应的通知方法
- (void)orientationHasChange:(NSNotification *)notification { if(!_currentPlayingCell){ return; } UIDevice *device = (UIDevice *)notification.object; if(device.orientation == UIInterfaceOrientationLandscapeLeft){ [self rotateToLandscapeLeft]; } else if(device.orientation == UIInterfaceOrientationLandscapeRight){ [self rotateToLandscapeRight]; } else if(device.orientation == UIInterfaceOrientationPortrait){ [self rotateToPortrait]; } }
最后要注意,离开页面时也要停止播放当前cell中的视频
- (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil]; if(_currentPlayingCell){ [_currentPlayingCell backToInitState]; _currentPlayingCell = nil; } }
OK,大功告成。
PS:项目完整版下载地址为:
https://github.com/wangqi211/VideoListPlayDemo
相关文章推荐
- DVI 视频接口图文解析
- C#实现语音视频录制-附demo源码
- C#调用mmpeg进行各种视频转换的类实例
- C#获取视频某一帧的缩略图的方法
- 显示youtube视频缩略图和Vimeo视频缩略图代码分享
- PHP使用ffmpeg给视频增加字幕显示的方法
- PHP实现将视频转成MP4并获取视频预览图的方法
- C++实现优酷土豆去视频广告的方法
- PHP简单获取视频预览图的方法
- asp.net 页面中添加普通视频的几种方式介绍
- Android获取SD卡上图片和视频缩略图的小例子
- php使用memcoder将视频转成mp4格式的方法
- 基于js与flash实现的网站flv视频播放插件代码
- java调用ffmpeg实现视频转换的方法
- js+HTML5基于过滤器从摄像头中捕获视频的方法
- python+ffmpeg视频并发直播压力测试
- 用Python的Django框架完成视频处理任务的教程
- UITableView 实现汽车品牌(demo)
- Nginx搭建流媒体FLV视频服务器配置示例
- [总结]视频质量评价技术零基础学习方法