Skip to content

UITableView及UICollectionView中播放的解决方案v3

changsanjiang edited this page Apr 28, 2021 · 7 revisions

required: SJBaseVideoPlayer >= 3.6.3

  • case 1: The controller contains a UITableView, and the UITableViewCell of the UITableView contains a player superview.
@interface YourViewController()
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) SJVideoPlayer *player;
// ...
@end

@implementation YourViewController
- (void)_playVideoAtIndexPath:(NSIndexPath *)indexPath {
    if ( !_player ) {
        _player = [SJVideoPlayer player];
    }
    
    SJPlayModel *playModel = [SJPlayModel playModelWithTableView:_tableView indexPath:indexPath superviewSelector:@selector(playerSuperview)];
    _player.URLAsset = [[SJVideoPlayerURLAsset alloc] initWithURL:URL playModel:playModel];
}
// ...
// ...
@end

@interface YourTableViewCell ()
@property (nonatomic, strong) UIView *playerSuperview;
// ...
@end

@implementation YourTableViewCell
// ...
@end

  • case 2: The controller contains a UICollectionView, and the UICollectionViewCell of the UICollectionView contains a player superview.
@interface YourViewController()
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) SJVideoPlayer *player;
// ...
@end

@implementation YourViewController
- (void)_playVideoAtIndexPath:(NSIndexPath *)indexPath {
    if ( !_player ) {
        _player = [SJVideoPlayer player];
    }
    
    SJPlayModel *playModel = [SJPlayModel playModelWithCollectionView:_collectionView indexPath:indexPath superviewSelector:@selector(playerSuperview)];
    _player.URLAsset = [[SJVideoPlayerURLAsset alloc] initWithURL:URL playModel:playModel];
}
// ...
// ...
@end

@interface YourCollectionViewCell ()
@property (nonatomic, strong) UIView *playerSuperview;
// ...
@end

@implementation YourCollectionViewCell
// ...
@end

case 3: The controller contains a UITableView, UITableViewCell of the UITableView contains a UICollectionView, and the UICollectionViewCell of the UICollectionView contains a player superview.

@interface YourViewController()
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) SJVideoPlayer *player;
// ...
@end

@implementation YourViewController
- (void)_playVideoForRowAtIndexPath:(NSIndexPath *)rowIndexPath itemIndexPath:(NSIndexPath *)itemIndexPath {
    if ( !_player ) {
        _player = [SJVideoPlayer player];
    }
    
    YourTableViewCell *tableViewCell = [_tableView cellForRowAtIndexPath:rowIndexPath];
    SJPlayModel *playModel = [SJPlayModel playModelWithCollectionView:tableViewCell.collectionView indexPath:itemIndexPath superviewSelector:@selector(playerSuperview)];

    SJPlayModel *next = [SJPlayModel playModelWithTableView:_tableView indexPath:rowIndexPath];
    next.scrollViewSelector = @selector(collectionView);
    playModel.nextPlayModel = next;
    _player.URLAsset = [[SJVideoPlayerURLAsset alloc] initWithURL:URL playModel:playModel];
}
// ...
// ...
@end


@interface YourTableViewCell ()
@property (nonatomic, strong) UICollectionView *collectionView;
// ...
@end

@implementation YourTableViewCell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    return YourCollectionViewCell;
}
// ...
@end


@interface YourCollectionViewCell ()
@property (nonatomic, strong) UIView *playerSuperview;
// ...
@end

@implementation YourCollectionViewCell
// ...
@end