多布局狀態(tài)下視頻自動(dòng)播放

今天主要說(shuō)的是節(jié)操視頻在多布局的狀態(tài)下自動(dòng)播放,由于布局沒(méi)有固定的規(guī)則所以不能只判斷位置來(lái)自動(dòng)播放,所以使用啦判斷子條目中的控件,而且,定義啦一個(gè)矩形的框框來(lái)判斷,如果,你的那個(gè)控件到達(dá)啦這個(gè)框框的范圍內(nèi),就開(kāi)始啦自動(dòng)播放
implementation 'fm.jiecao:jiecaovideoplayer:5.4'

正常的滑動(dòng)監(jiān)聽(tīng)

        rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                //playVideo就是開(kāi)啟自動(dòng)播放的方法,另外,一般會(huì)添加一個(gè)判斷是不是wifi狀態(tài)下
                if (newState == SCROLL_STATE_IDLE) playVideo(recyclerView);
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);滑動(dòng)范圍
                //可以獲取屏幕中展示第一個(gè)item的position值
                int firstPosition = mLayoutManager.findFirstVisibleItemPosition();
                // 可以獲取屏幕中展示最后一個(gè)item的position值
                int lastPosition = mLayoutManager.findLastVisibleItemPosition();
               //相減就是我們當(dāng)前看到問(wèn)獲取屏幕中item的條目數(shù)
                visibleCount = lastPosition - firstPosition;
            }
        });
    private void playVideo(RecyclerView view) {
        for (int i = 0; i < visibleCount; i++) {
            if (view != null && view.getChildAt(i) != null && view.getChildAt(i).findViewById(R.id.re_shi_kai) != null) {
                JCVideoPlayerStandard videoPlayer = view.getChildAt(i).findViewById(R.id.re_shi_kai);
                Rect rect = new Rect();
                //getLocalVisibleRect(Rect r)方法可以把視圖的長(zhǎng)和寬映射到一個(gè)Rect對(duì)象上。
                videoPlayer.getLocalVisibleRect(rect);
                int height = videoPlayer.getHeight();
                if (rect.top == 0 && rect.bottom == height) {
                    if (videoPlayer.currentState == JCVideoPlayer.CURRENT_STATE_NORMAL || videoPlayer.currentState == JCVideoPlayer.CURRENT_STATE_ERROR) {
                        videoPlayer.startButton.performClick();
                           //這個(gè)是節(jié)操視頻自帶的開(kāi)啟方式                         
//                        videoPlayer.startVideo();   
                        BaseApp.getmApplication().mVideoPlayer = videoPlayer;
                    }
                    return;
                }
            }
        }
        JCVideoPlayer.releaseAllVideos();
        BaseApp.getmApplication().mVideoPlayer = null;
    }

我的是在fragment中的多布局 所以在依附的activity暫停失去焦點(diǎn)是要將節(jié)操視頻關(guān)閉

  @Override
    protected void onPause() {
        super.onPause();
         /*
         *  我的是在fragment中的多布局 所以在依附的activity暫停失去焦點(diǎn)是要將節(jié)操視頻關(guān)閉
         *      public static JCVideoPlayerStandard mVideoPlayer;
         *  mVideoPlayer是在BaseApp是需要定義個(gè)靜態(tài)的mVideoPlayer方法就行啦  為了保持在一個(gè)視頻對(duì)象
         * */
        if (BaseApp.mVideoPlayer != null) {
            if (BaseApp.mVideoPlayer.currentState == JCVideoPlayer.CURRENT_STATE_PLAYING) {
                BaseApp.mVideoPlayer.startButton.performClick();
               // JCMediaManager.instance().mediaPlayer.pause();
            } else if (BaseApp.mVideoPlayer.currentState == JCVideoPlayer.CURRENT_STATE_PREPARING) {
                JCVideoPlayer.releaseAllVideos();
            }
        }
    }

但是我的fragment的也是復(fù)用的所以在切換時(shí)也需要關(guān)閉啦視頻的播放

代碼和上面一樣
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容