| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- //
- // ZFCollectionViewController.m
- // ZFPlayer_Example
- //
- // Created by 任子丰 on 2018/6/21.
- // Copyright © 2018年 紫枫. All rights reserved.
- //
- #import "ZFCollectionViewController.h"
- #import "ZFCollectionViewCell.h"
- #import "ZFTableData.h"
- #import <ZFPlayer/ZFAVPlayerManager.h>
- #import <ZFPlayer/ZFPlayerControlView.h>
- #import <ZFPlayer/UIView+ZFFrame.h>
- #import <ZFPlayer/ZFPlayerConst.h>
- static NSString * const reuseIdentifier = @"collectionViewCell";
- @interface ZFCollectionViewController () <UICollectionViewDelegate,UICollectionViewDataSource>
- @property (nonatomic, strong) NSMutableArray <ZFTableData *>*dataSource;
- @property (nonatomic, strong) UICollectionView *collectionView;
- @property (nonatomic, strong) ZFPlayerController *player;
- @property (nonatomic, strong) ZFPlayerControlView *controlView;
- @end
- @implementation ZFCollectionViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor whiteColor];
- [self.view addSubview:self.collectionView];
- [self requestData];
-
- /// playerManager
- ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
- // ZFIJKPlayerManager *playerManager = [[ZFIJKPlayerManager alloc] init];
-
- /// player的tag值必须在cell里设置
- self.player = [ZFPlayerController playerWithScrollView:self.collectionView playerManager:playerManager containerViewTag:kPlayerViewTag];
- self.player.controlView = self.controlView;
- self.player.shouldAutoPlay = YES;
-
- @zf_weakify(self)
- self.player.playerDidToEnd = ^(id _Nonnull asset) {
- @zf_strongify(self)
- if (self.player.playingIndexPath.row < self.dataSource.count - 1) {
- NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.player.playingIndexPath.row+1 inSection:0];
- [self playTheVideoAtIndexPath:indexPath scrollAnimated:YES];
- } else {
- [self.player.currentPlayerManager replay];
- }
- };
-
- /// 停止的时候找出最合适的播放
- self.player.zf_scrollViewDidEndScrollingCallback = ^(NSIndexPath * _Nonnull indexPath) {
- @zf_strongify(self)
- [self playTheVideoAtIndexPath:indexPath scrollAnimated:NO];
- };
-
- /*
-
- /// 滑动中找到适合的就自动播放
- /// 如果是停止后再寻找播放可以忽略这个回调
- /// 如果在滑动中就要寻找到播放的indexPath,并且开始播放,那就要这样写
- self.player.zf_playerShouldPlayInScrollView = ^(NSIndexPath * _Nonnull indexPath) {
- @zf_strongify(self)
- if ([indexPath compare:self.player.playingIndexPath] != NSOrderedSame) {
- [self playTheVideoAtIndexPath:indexPath scrollAnimated:NO];
- }
- };
-
- */
- }
- - (void)viewWillLayoutSubviews {
- [super viewWillLayoutSubviews];
- self.collectionView.frame = self.view.bounds;
- }
- - (void)viewDidAppear:(BOOL)animated {
- [super viewDidAppear:animated];
- @zf_weakify(self)
- [self.player zf_filterShouldPlayCellWhileScrolled:^(NSIndexPath *indexPath) {
- @zf_strongify(self)
- [self playTheVideoAtIndexPath:indexPath scrollAnimated:NO];
- }];
- }
- #pragma mark - 转屏和状态栏
- - (BOOL)shouldAutorotate {
- return NO;
- }
- - (UIStatusBarStyle)preferredStatusBarStyle {
- return UIStatusBarStyleDefault;
- }
- - (BOOL)prefersStatusBarHidden {
- return NO;
- }
- #pragma mark - private method
- - (void)requestData {
- NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
- NSData *data = [NSData dataWithContentsOfFile:path];
- NSDictionary *rootDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
-
- self.dataSource = @[].mutableCopy;
- NSArray *videoList = [rootDict objectForKey:@"list"];
- for (NSDictionary *dataDic in videoList) {
- ZFTableData *data = [[ZFTableData alloc] init];
- [data setValuesForKeysWithDictionary:dataDic];
- [self.dataSource addObject:data];
- }
- }
- /// play the video
- - (void)playTheVideoAtIndexPath:(NSIndexPath *)indexPath scrollAnimated:(BOOL)animated {
- ZFTableData *data = self.dataSource[indexPath.row];
- if (animated) {
- [self.player playTheIndexPath:indexPath assetURL:[NSURL URLWithString:data.video_url] scrollPosition:ZFPlayerScrollViewScrollPositionCenteredVertically animated:YES];
- } else {
- [self.player playTheIndexPath:indexPath assetURL:[NSURL URLWithString:data.video_url]];
- }
- [self.controlView showTitle:data.title
- coverURLString:data.thumbnail_url
- fullScreenMode:ZFFullScreenModeLandscape];
- }
- #pragma mark - UIScrollViewDelegate 列表播放必须实现
- - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
- [scrollView zf_scrollViewDidEndDecelerating];
- }
- - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
- [scrollView zf_scrollViewDidEndDraggingWillDecelerate:decelerate];
- }
- - (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
- [scrollView zf_scrollViewDidScrollToTop];
- }
- - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
- [scrollView zf_scrollViewDidScroll];
- }
- - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
- [scrollView zf_scrollViewWillBeginDragging];
- }
- #pragma mark UICollectionViewDataSource
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- return self.dataSource.count;
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
- ZFCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
- cell.data = self.dataSource[indexPath.row];
- @zf_weakify(self)
- cell.playBlock = ^(UIButton *sender) {
- @zf_strongify(self)
- [self playTheVideoAtIndexPath:indexPath scrollAnimated:NO];
- };
- return cell;
- }
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
- [self playTheVideoAtIndexPath:indexPath scrollAnimated:NO];
- }
- - (UICollectionView *)collectionView {
- if (!_collectionView) {
- UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
- CGFloat margin = 5;
- CGFloat itemWidth = self.view.frame.size.width;
- CGFloat itemHeight = itemWidth*9/16 + 30;
- layout.itemSize = CGSizeMake(itemWidth, itemHeight);
- layout.sectionInset = UIEdgeInsetsMake(10, margin, 10, margin);
- layout.minimumLineSpacing = 5;
- layout.minimumInteritemSpacing = 5;
- _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
- _collectionView.delegate = self;
- _collectionView.dataSource = self;
- _collectionView.backgroundColor = [UIColor whiteColor];
- [_collectionView registerClass:[ZFCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
- }
- return _collectionView;
- }
- - (ZFPlayerControlView *)controlView {
- if (!_controlView) {
- _controlView = [ZFPlayerControlView new];
- }
- return _controlView;
- }
- @end
|