基于JPVideoPlayerKit的小视频播放

2019-06-26  本文已影响0人  七月不下雨
视频随便找的

//大部分都是复制过来的源码。。

@interface JPDouyinProgressView: JPVideoPlayerProgressView

@end

@implementation JPDouyinProgressView

- (void)layoutThatFits:(CGRect)constrainedRect
nearestViewControllerInViewTree:(UIViewController *_Nullable)nearestViewController
 interfaceOrientation:(JPVideoPlayViewInterfaceOrientation)interfaceOrientation {
   [super layoutThatFits:constrainedRect
nearestViewControllerInViewTree:nearestViewController
    interfaceOrientation:interfaceOrientation];
   
   self.trackProgressView.frame = CGRectMake(0,
                                             constrainedRect.size.height - JPVideoPlayerProgressViewElementHeight,
                                             constrainedRect.size.width,
                                             JPVideoPlayerProgressViewElementHeight);
   self.cachedProgressView.frame = self.trackProgressView.bounds;
   self.elapsedProgressView.frame = self.trackProgressView.frame;
   
}

@end
@implementation HFVideoListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//禁止侧滑,防止不必要的bug。
    id traget = self.navigationController.interactivePopGestureRecognizer.delegate;
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:traget action:nil];
    [self.view addGestureRecognizer:pan];
    
 
    
    
    
    [self setup];
//视图全屏
    self.scrollView.contentInset = UIEdgeInsetsZero;
    if (@available(iOS 11.0, *)) {
        self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    self.scrollViewOffsetYOnStartDrag = -1;
    [self scrollViewDidEndScrolling];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.scrollView.contentInset = UIEdgeInsetsZero;
    if (@available(iOS 11.0, *)) {
        self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [IQKeyboardManager sharedManager].enable = NO;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [IQKeyboardManager sharedManager].enable = YES;
}


- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    

}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
    [self.secondImageView jp_stopPlay];
}


#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView
                  willDecelerate:(BOOL)decelerate {
    if (decelerate == NO) {
        [self scrollViewDidEndScrolling];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self scrollViewDidEndScrolling];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
        NSLog(@"❤️scrollviewY%f>>>startDrag%f", self.scrollView.contentOffset.y,self.scrollViewOffsetYOnStartDrag);
    self.scrollViewOffsetYOnStartDrag = scrollView.contentOffset.y;
}


#pragma mark - JPVideoPlayerDelegate

- (BOOL)shouldShowBlackBackgroundBeforePlaybackStart {
    return YES;
}

#pragma mark - Private

- (void)scrollViewDidEndScrolling {
    
        NSLog(@"❤️❤️scrollviewY%f>>>startDrag%f", self.scrollView.contentOffset.y,self.scrollViewOffsetYOnStartDrag);
    if(self.scrollViewOffsetYOnStartDrag == self.scrollView.contentOffset.y){
        return;
    }
    //移除小视频上面展示的用户信息
    [self removeUserInfoViews];
    
 
    
    
    NSURL *url ;
    MKSmallVideoListModel *model;
    if (self.scrollViewOffsetYOnStartDrag == -1) {//只有刚进来的时候调用此方法,此currentindex是上级小视频列表传过来的下标。
        NSLog(@"-1>>>%ld",self.currentVideoIndex);
        model = self.ListArr[self.currentVideoIndex];
//        url = [NSURL URLWithString:model.video];
         url = sdAppendImage(model.video);

    }else{
//判断上滑还是下滑
        if (self.scrollView.contentOffset.y==0) {
            if(self.currentVideoIndex == 0){
    
                self.currentVideoIndex = (self.ListArr.count - 1);
            }else{
                self.currentVideoIndex--;
     
            }
            if (self.currentVideoIndex < 0) {
                self.currentVideoIndex = 0;
            }
            model = self.ListArr[self.currentVideoIndex];
            
            url = sdAppendImage(model.video);
//            url = [NSURL URLWithString:model.video];
            
            
        }else{
            
            if(self.currentVideoIndex == (self.ListArr.count - 1)){
                self.currentVideoIndex = 0;
            }else{
                self.currentVideoIndex++;
                
            }
            if (self.currentVideoIndex >= self.ListArr.count) {
                self.currentVideoIndex = self.ListArr.count - 1;
            }
            
            model = self.ListArr[self.currentVideoIndex];
             url = sdAppendImage(model.video);
//            url = [NSURL URLWithString:model.video];
//            url = [NSURL URLWithString:self.douyinVideoStrings[self.currentVideoIndex]];
            
            
        }
    }

    
    CGSize referenceSize = UIScreen.mainScreen.bounds.size;
    [self.scrollView setContentOffset:CGPointMake(0, referenceSize.height) animated:NO];

    [self.secondImageView jp_stopPlay];
//设置当前视频的顶层封面。。
    _secondImageView1.hidden = NO;
    _secondImageView1.alpha = 1;

    
    
    [self.secondImageView jp_playVideoMuteWithURL:url
                               bufferingIndicator:nil
                                     progressView:[JPDouyinProgressView new]
                                    configuration:^(UIView *view, JPVideoPlayerModel *playerModel) {
                                        view.jp_muted = NO;
//                                        view.backgroundColor = [UIColor clearColor];
                                    }];

    
    self.secondImageView.jp_progressView.hidden = YES;
    
    
 
    
    // 添加小视频上面展示的用户信息并配置数据
    [self setupUserInfoViews:model];
    //配置视频封面
    [self configVideoThumb];
    
}

- (void)configVideoThumb{
    
    if (self.ListArr.count > _currentVideoIndex) {

        
        MKSmallVideoListModel *currentModel = self.ListArr[_currentVideoIndex];
        [self.secondImageView1 sd_setImageWithURL:sdAppendImage(currentModel.thumb) placeholderImage:nil];
        
    }
    
    NSInteger lastIndex = _currentVideoIndex-1;
    if (lastIndex < 0) {
        lastIndex = self.ListArr.count-1;
    }
    MKSmallVideoListModel *lastModel = self.ListArr[lastIndex];
    [self.firstImageView sd_setImageWithURL:sdAppendImage(lastModel.thumb) placeholderImage:nil];
    
    
    
    NSInteger nextIndex  =_currentVideoIndex+1;
    if (nextIndex >= self.ListArr.count) {
        nextIndex = 0;
    }
    
    MKSmallVideoListModel *nextModel = self.ListArr[nextIndex];
    [self.thridImageView sd_setImageWithURL:sdAppendImage(nextModel.thumb) placeholderImage:nil];
}

#pragma mark - Setup

- (void)setup {
    self.view.backgroundColor = [UIColor whiteColor];
    CGSize referenceSize = UIScreen.mainScreen.bounds.size;
//    self.currentVideoIndex = 0;
    
    self.scrollView = ({
        UIScrollView *scrollView = [UIScrollView new];
        scrollView.backgroundColor = [UIColor blackColor];
        [self.view addSubview:scrollView];
        scrollView.frame = self.view.bounds;
        scrollView.contentSize = CGSizeMake(referenceSize.width, referenceSize.height * 3);
        scrollView.pagingEnabled = YES;
        scrollView.delegate = self;
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.showsVerticalScrollIndicator  = NO;
        scrollView;
    });
    
    self.firstImageView = ({
        UIImageView *imageView = [UIImageView new];
        [self.scrollView addSubview:imageView];
        imageView.frame = CGRectMake(0, 0, referenceSize.width, referenceSize.height);
//        imageView.image = [UIImage imageNamed:@"mine_redcoupon_redn"];
        imageView.userInteractionEnabled = YES;
        imageView;
    });
    
    self.secondImageView = ({
        UIImageView *imageView = [UIImageView new];
        [self.scrollView addSubview:imageView];
        imageView.frame = CGRectMake(0, referenceSize.height, referenceSize.width, referenceSize.height);
//        imageView.image = [UIImage imageNamed:@"placeholder2"];
        imageView.jp_videoPlayerDelegate = self;
        imageView.userInteractionEnabled = YES;
        imageView;
    });
    
    self.secondImageView1 = ({
        UIImageView *imageView = [UIImageView new];
        [self.scrollView addSubview:imageView];
        imageView.frame = CGRectMake(0, referenceSize.height, referenceSize.width, referenceSize.height);
        //        imageView.image = [UIImage imageNamed:@"placeholder2"];
        imageView.userInteractionEnabled = YES;
        imageView;
    });
    
    
    
    self.thridImageView = ({
        UIImageView *imageView = [UIImageView new];
        [self.scrollView addSubview:imageView];
        imageView.frame = CGRectMake(0, referenceSize.height * 2, referenceSize.width, referenceSize.height);
//        imageView.image = [UIImage imageNamed:@"mine_redcoupon_redd"];
        imageView.userInteractionEnabled = YES;
        imageView;
    });
    
    self.backBtn = ({
        UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [btn setImage:[UIImage imageNamed:@"navi_back_white"] forState:(UIControlStateNormal)];
        [btn setFrame:CGRectMake(10, IPHONE_X?44:20, 44, 44)];
        [btn addTarget:self action:@selector(backBtnClick) forControlEvents:(UIControlEventTouchUpInside)];
        [self.view addSubview:btn];
        btn;
    });
    
}
#pragma mark --↓-- Videodelegate --↓--
- (void)playerStatusDidChanged:(JPVideoPlayerStatus)playerStatus {
//解决视频加载后有一个黑色闪动的问题,视频大的话,还是有这种问题,目前没有好的解决方法。
    if (playerStatus == JPVideoPlayerStatusPlaying) {
        
        [UIView animateWithDuration:0.15 animations:^{
            self.secondImageView1.alpha = 0;
          
        }completion:^(BOOL finished) {
              self.secondImageView1.hidden = YES;
        }];

        
    }
}

- (void)setupUserInfoViews:(MKSmallVideoListModel *)model {
    _tempModel = model;
    
    [self.secondImageView addSubview:self.iconImgView];
}
上一篇 下一篇

猜你喜欢

热点阅读