// // AVVodListViewController.m // SellyCloudSDK_Example // #import "AVVodListViewController.h" #import "AVVodItemModel.h" #import "AVVodItemCell.h" #import "AVLiveStreamModel.h" #import "SCVodVideoPlayerViewController.h" #import "AVConstants.h" #import @interface AVVodListViewController () @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) NSArray *vodItems; @end @implementation AVVodListViewController static NSString * const kVodItemCellIdentifier = @"VodItemCell"; - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor systemBackgroundColor]; self.title = @"点播测试"; self.vodItems = @[]; [self setupCollectionView]; [self fetchVodList]; } #pragma mark - Network - (void)fetchVodList { NSURL *url = [NSURL URLWithString:@"http://rtmp.sellycloud.io:8089/live/sdk/demo/vodlist"]; NSURLSessionDataTask *task = [NSURLSession.sharedSession dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { if (error || !data) { NSLog(@"fetchVodList failed: %@", error); return; } NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; if (![json isKindOfClass:NSDictionary.class]) return; NSMutableArray *items = [NSMutableArray array]; [json enumerateKeysAndObjectsUsingBlock:^(NSString *type, NSString *vodUrl, BOOL *stop) { AVVodItemModel *item = [AVVodItemModel modelWithUrl:vodUrl cover:@"" title:[NSString stringWithFormat:@"%@ 测试", type.uppercaseString] type:type]; [items addObject:item]; }]; dispatch_async(dispatch_get_main_queue(), ^{ self.vodItems = items.copy; [self.collectionView reloadData]; }); }]; [task resume]; } #pragma mark - Setup UI - (void)setupCollectionView { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.scrollDirection = UICollectionViewScrollDirectionVertical; layout.minimumInteritemSpacing = 12; layout.minimumLineSpacing = 16; layout.sectionInset = UIEdgeInsetsMake(16, 16, 16, 16); self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; self.collectionView.backgroundColor = [UIColor systemBackgroundColor]; self.collectionView.delegate = self; self.collectionView.dataSource = self; self.collectionView.alwaysBounceVertical = YES; [self.collectionView registerClass:[AVVodItemCell class] forCellWithReuseIdentifier:kVodItemCellIdentifier]; [self.view addSubview:self.collectionView]; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop); make.left.right.bottom.equalTo(self.view); }]; } #pragma mark - UICollectionViewDataSource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.vodItems.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { AVVodItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kVodItemCellIdentifier forIndexPath:indexPath]; [cell configureWithModel:self.vodItems[indexPath.item]]; return cell; } #pragma mark - UICollectionViewDelegateFlowLayout - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)layout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { CGFloat totalHorizontalPadding = 16 * 2 + 12; CGFloat availableWidth = collectionView.bounds.size.width - totalHorizontalPadding; CGFloat itemWidth = availableWidth / 2.0; CGFloat thumbnailHeight = itemWidth * (9.0 / 16.0); CGFloat itemHeight = thumbnailHeight + 6 + 18 + 6; // thumbnail + spacing + title (1 line) + bottom return CGSizeMake(itemWidth, itemHeight); } #pragma mark - UICollectionViewDelegate - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { [collectionView deselectItemAtIndexPath:indexPath animated:YES]; AVVodItemModel *item = self.vodItems[indexPath.item]; // Convert to AVLiveStreamModel for the player AVLiveStreamModel *stream = [[AVLiveStreamModel alloc] init]; stream.url = item.url; SCVodVideoPlayerViewController *vc = [[SCVodVideoPlayerViewController alloc] initWithLiveStream:stream]; vc.hidesBottomBarWhenPushed = YES; vc.modalPresentationStyle = UIModalPresentationFullScreen; [self.navigationController pushViewController:vc animated:YES]; } #pragma mark - Orientation Support - (BOOL)shouldAutorotate { return YES; } - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; } @end