基于JPVideoPlayerKit的小视频播放

视频随便找的

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

@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];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,816评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,729评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,300评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,780评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,890评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,084评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,151评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,912评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,355评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,666评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,809评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,504评论 4 334
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,150评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,882评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,121评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,628评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,724评论 2 351