类似搜狐视频app视频列表播放

5 篇文章 0 订阅

有些视频app有一个视频列表播放功能,例如搜狐视频app的热点模块,腾讯视频app的热点模块等。进入此页面会自动播放视频,滑动页面还会自动切换播放视频,同时支持横竖屏切换,确实比较方便。研究了下,实现方法可以如下。

首先记录下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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值