iOS仿抖音上下滑動播放視頻

首先看下效果圖 git

圖作的不太好,將就看吧

前言

上一篇文章仿寫了抖音的左右滑動效果-iOS之仿抖音左右滑動效果,有興趣的能夠去GKNavigationBarViewController的demo中查看。 這篇文章主要是對抖音的上下滑動及視頻播放功能作介紹。其中上下滑動用的是UIScrollview,包含3個子視圖。視頻播放用的是TX的獨立播放器TXLiteAVSDK_Player,可實現音視頻的點播、直播等功能。github

demo中的視頻是經過抓包獲取的百度的夥拍小視頻,僅供學習使用。bash

說明

一、上下滑動切換視圖實現

主要是在UIScrollview的代理中作處理,且對滑動到第一個和最後一個時分開處理,看下代碼ide

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // 小於等於三個,不用處理
    if (self.videos.count <= 3) return;
    
    // 上滑到第一個
    if (self.index == 0 && scrollView.contentOffset.y <= SCREEN_HEIGHT) {
        return;
    }
    // 下滑到最後一個
    if (self.index == self.videos.count - 1 && scrollView.contentOffset.y > SCREEN_HEIGHT) {
        return;
    }
    
    // 判斷是從中間視圖上滑仍是下滑
    if (scrollView.contentOffset.y >= 2 * SCREEN_HEIGHT) {  // 上滑
        [self.player removeVideo];  // 在這裏移除播放,解決閃動的bug
        if (self.index == 0) {
            self.index += 2;
            
            scrollView.contentOffset = CGPointMake(0, SCREEN_HEIGHT);
            
            self.topView.model = self.ctrView.model;
            self.ctrView.model = self.btmView.model;
            
        }else {
            self.index += 1;
            
            if (self.index == self.videos.count - 1) {
                self.ctrView.model = self.videos[self.index - 1];
            }else {
                scrollView.contentOffset = CGPointMake(0, SCREEN_HEIGHT);
                
                self.topView.model = self.ctrView.model;
                self.ctrView.model = self.btmView.model;
            }
        }
        if (self.index < self.videos.count - 1) {
            self.btmView.model = self.videos[self.index + 1];
        }
    }else if (scrollView.contentOffset.y <= 0) { // 下滑
        [self.player removeVideo];  // 在這裏移除播放,解決閃動的bug
        if (self.index == 1) {
            self.topView.model = self.videos[self.index - 1];
            self.ctrView.model = self.videos[self.index];
            self.btmView.model = self.videos[self.index + 1];
            self.index -= 1;
        }else {
            if (self.index == self.videos.count - 1) {
                self.index -= 2;
            }else {
                self.index -= 1;
            }
            scrollView.contentOffset = CGPointMake(0, SCREEN_HEIGHT);
            
            self.btmView.model = self.ctrView.model;
            self.ctrView.model = self.topView.model;
            
            if (self.index > 0) {
                self.topView.model = self.videos[self.index - 1];
            }
        }
    }
}
複製代碼

滑動結束後開始播放oop

// 結束滾動後開始播放
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y == 0) {
        if (self.currentPlayId == self.topView.model.post_id) return;
        [self playVideoFrom:self.topView];
    }else if (scrollView.contentOffset.y == SCREEN_HEIGHT) {
        if (self.currentPlayId == self.ctrView.model.post_id) return;
        [self playVideoFrom:self.ctrView];
    }else if (scrollView.contentOffset.y == 2 * SCREEN_HEIGHT) {
        if (self.currentPlayId == self.btmView.model.post_id) return;
        [self playVideoFrom:self.btmView];
    }
    
    if (self.isPushed) return;
    
    // 當只剩最後兩個的時候,獲取新數據
    if (self.currentPlayIndex == self.videos.count - 2) {
        [self.viewModel refreshNewListWithSuccess:^(NSArray * _Nonnull list) {
            [self.videos addObjectsFromArray:list];
        } failure:^(NSError * _Nonnull error) {
            NSLog(@"%@", error);
        }];
    }
}
複製代碼

上下滑動的處理基本就這些,若是有不懂的能夠下載demo查看。post

二、播放器封裝

這裏封裝的播放器用的是騰訊的視頻點播TXVodPlayer。 建立播放器學習

- (TXVodPlayer *)player {
    if (!_player) {
        [TXLiveBase setLogLevel:LOGLEVEL_NULL];
        [TXLiveBase setConsoleEnabled:NO];
        
        _player = [TXVodPlayer new];
        _player.vodDelegate = self;
        _player.loop = YES; // 開啓循環播放功能
    }
    return _player;
}
複製代碼

因爲是多個視頻切換播放,因此最好只用一個播放器,所以在切換視圖後,須要播放器切換播放視圖和播放地址,因此提供了下面的方法。ui

- (void)playVideoWithView:(UIView *)playView url:(NSString *)url {
    // 設置播放視圖
    [self.player setupVideoWidget:playView insertIndex:0];
    
    // 準備播放
    [self playerStatusChanged:GKDYVideoPlayerStatusPrepared];
    
    // 開始播放
    if ([self.player startPlay:url] == 0) {
        // 這裏可加入緩衝視圖
    }else {
        [self playerStatusChanged:GKDYVideoPlayerStatusError];
    }
}
複製代碼

播放器狀態監聽,可獲取播放狀態及進度等url

- (void)onPlayEvent:(TXVodPlayer *)player event:(int)EvtID withParam:(NSDictionary *)param {
    switch (EvtID) {
        case PLAY_EVT_PLAY_LOADING:{    // loading
            if (self.status == GKDYVideoPlayerStatusPaused) {
                [self playerStatusChanged:GKDYVideoPlayerStatusPaused];
            }else {
                [self playerStatusChanged:GKDYVideoPlayerStatusLoading];
            }
        }
            break;
        case PLAY_EVT_PLAY_BEGIN:{    // 開始播放
            [self playerStatusChanged:GKDYVideoPlayerStatusPlaying];
        }
            break;
        case PLAY_EVT_PLAY_END:{    // 播放結束
            [self playerStatusChanged:GKDYVideoPlayerStatusEnded];
        }
            break;
        case PLAY_ERR_NET_DISCONNECT:{    // 失敗,屢次重連無效
            [self playerStatusChanged:GKDYVideoPlayerStatusError];
        }
            break;
        case PLAY_EVT_PLAY_PROGRESS:{    // 進度
            if (self.status == GKDYVideoPlayerStatusPlaying) {
                self.duration = [param[EVT_PLAY_DURATION] floatValue];
                
                float currTime = [param[EVT_PLAY_PROGRESS] floatValue];
                
                float progress = self.duration == 0 ? 0 : currTime / self.duration;
                
                // 處理播放結束時,進度不更新問題
                if (progress >= 0.95) progress = 1.0f;
                
                //                float buffTime = [param[EVT_PLAYABLE_DURATION] floatValue];
                //                float burrProgress = self.duration == 0 ? 0 : buffTime / self.duration;
                
                if ([self.delegate respondsToSelector:@selector(player:currentTime:totalTime:progress:)]) {
                    [self.delegate player:self currentTime:currTime totalTime:self.duration progress:progress];
                }
            }
        }
            break;
            
        default:
            break;
    }
}
複製代碼

內容比較多,若是想要具體瞭解,還須要下載代碼查看。spa

最後

demo的地址:GKDYVideo,因爲github的限制,播放器沒法上傳,因此須要下載下來後pod install便可。

相關文章
相關標籤/搜索