SuperPlayer:基于 IjkPlayer 的播放器

ldffkal 8年前發布 | 17K 次閱讀 安卓開發 Android開發 移動開發

超級播放器

SuperPlayer是一個基于IjkPlayer的控制器,支持手勢操作,滑動快進,快退,支持,上滑音量亮度的變化,支持指點位置播放,播放源的切換,親測可用,廢話少說,先上圖

Screenshots

How to User

gradle

ps:還有時間上傳到maven,過幾天在上傳上去,喜歡的可以直接Clone or download

視頻點播(直播)功能(播放網絡視頻,或者視頻直播)

設置視頻源(視頻地址)是否是直播還是點播

player.setLive(true);//true:表示直播地址;false表示點播地址

設置播放過程中是否監聽網絡的變化

player.setNetChangeListener(true);//true : 表示監聽網絡的變化;false : 播放的過程中不監聽網絡的變化

設置播放過程中的視頻開始播放

player.onPrepared(new SuperPlayer.OnPreparedListener() {
                @Override
                public void onPrepared() {
                    /**

                 * 監聽視頻是否已經準備完成開始播放。(可以在這里處理視頻封面的顯示跟隱藏)
                 */
            }
        })</code></pre> 

設置視頻是否已經播放完成

player.onComplete(new Runnable() {
                @Override
                public void run() {
                    /**
                     * 監聽視頻是否已經播放完成了。(可以在這里處理視頻播放完成進行的操作)
                     */
                }
            })

設置視頻播放失敗的監聽

player.onError(new SuperPlayer.OnErrorListener() {
                @Override
                public void onError(int what, int extra) {
                    /**
                     * 監聽視頻播放失敗的回調
                     */

                }
            })

設置視頻播放的信息

player.onInfo(new SuperPlayer.OnInfoListener() {
                @Override
                public void onInfo(int what, int extra) {
                    /**
                     * 監聽視頻的相關信息。
                     */

                }
            })

設置視頻播放的標題

player.setTitle(url)//設置視頻的titleName

設置視頻播放大小

player.setScaleType(SuperPlayer.SCALETYPE_FITXY);
其中可以設置
/**
 * fitParent:scale the video uniformly (maintain the video's aspect ratio)
 * so that both dimensions (width and height) of the video will be equal to
 * or **less** than the corresponding dimension of the view. like
 * ImageView's `CENTER_INSIDE`.等比縮放,畫面填滿view。
 */
public static final String SCALETYPE_FITPARENT = "fitParent";
/**
 * fillParent:scale the video uniformly (maintain the video's aspect ratio)
 * so that both dimensions (width and height) of the video will be equal to
 * or **larger** than the corresponding dimension of the view .like
 * ImageView's `CENTER_CROP`.等比縮放,直到畫面寬高都等于或小于view的寬高。
 */
public static final String SCALETYPE_FILLPARENT = "fillParent";
/**
 * wrapContent:center the video in the view,if the video is less than view
 * perform no scaling,if video is larger than view then scale the video
 * uniformly so that both dimensions (width and height) of the video will be
 * equal to or **less** than the corresponding dimension of the view.
 * 將視頻的內容完整居中顯示,如果視頻大于view,則按比例縮視頻直到完全顯示在view中。
 */
public static final String SCALETYPE_WRAPCONTENT = "wrapContent";
/**
 * fitXY:scale in X and Y independently, so that video matches view
 * exactly.不剪裁,非等比例拉伸畫面填滿整個View
 */
public static final String SCALETYPE_FITXY = "fitXY";
/**
 * 16:9:scale x and y with aspect ratio 16:9 until both dimensions (width
 * and height) of the video will be equal to or **less** than the
 * corresponding dimension of the view.不剪裁,非等比例拉伸畫面到16:9,并完全顯示在View中。
 */
public static final String SCALETYPE_16_9 = "16:9";
/**
 * 4:3:scale x and y with aspect ratio 4:3 until both dimensions (width and
 * height) of the video will be equal to or **less** than the corresponding
 * dimension of the view.不剪裁,非等比例拉伸畫面到4:3,并完全顯示在View中。
 */
public static final String SCALETYPE_4_3 = "4:3";

設置視頻播放的手勢操作

/**
 * 設置小屏幕是否支持手勢操作(默認false)
 * @param isSupportGesture
 *              true : 支持(小屏幕支持,大屏幕支持)
 *              false :不支持(小屏幕不支持,大屏幕支持)
 * @return
 */
public SuperPlayer setSupportGesture(boolean isSupportGesture){
    this.isSupportGesture = isSupportGesture;
    return this;
}

還有很多的相關設置,就不一一的列出。具體的請看SuperPlayer.class

列表播放器

很多的設置跟點播直播的一致。但是比較應該注意的地方

其一:初始化適配器

private void initAdapter() {
    mAdapter = new SuperVideoAdapter(this,dataList);
    superRecyclerView.setAdapter(mAdapter);
    mAdapter.setPlayClick(new SuperVideoAdapter.onPlayClick() {
        @Override
        public void onPlayclick(int position, RelativeLayout image) {
            image.setVisibility(View.GONE);
            if (player.isPlaying() && lastPostion == position){
                return;
            }

            postion = position;
            if (player.getVideoStatus() == IjkVideoView.STATE_PAUSED) {

                if (position != lastPostion) {
                    player.stopPlayVideo();
                    player.release();
                }
            }
            if (lastPostion != -1) {
                player.showView(R.id.adapter_player_control);
            }

            View view = superRecyclerView.findViewHolderForAdapterPosition(position).itemView;
            FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.adapter_super_video);
            frameLayout.removeAllViews();
            player.showView(R.id.adapter_player_control);
            frameLayout.addView(player);
            player.play(dataList.get(position).getVideoUrl());
            lastPostion = position;
        }
    });
    /**
     * 播放完設置還原播放界面
     */
    player.onComplete(new Runnable() {
        @Override
        public void run() {
            ViewGroup last = (ViewGroup) player.getParent();//找到videoitemview的父類,然后remove
            if (last != null && last.getChildCount() > 0) {
                last.removeAllViews();
                View itemView = (View) last.getParent();
                if (itemView != null) {
                    itemView.findViewById(R.id.adapter_player_control).setVisibility(View.VISIBLE);
                }
            }
        }
    });
    /***
     * 監聽列表的下拉滑動
     */
    superRecyclerView.addOnChildAttachStateChangeListener(new SuperRecyclerView.OnChildAttachStateChangeListener() {
        @Override
        public void onChildViewAttachedToWindow(View view) {
            int index = superRecyclerView.getChildAdapterPosition(view);
            View controlview = view.findViewById(R.id.adapter_player_control);
            if (controlview == null) {
                return;
            }
            view.findViewById(R.id.adapter_player_control).setVisibility(View.VISIBLE);
            if (index == postion) {
                FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.adapter_super_video);
                frameLayout.removeAllViews();
                if (player != null &&
                        ((player.isPlaying()) || player.getVideoStatus() == IjkVideoView.STATE_PAUSED)) {
                    view.findViewById(R.id.adapter_player_control).setVisibility(View.GONE);
                }
                if (player.getVideoStatus() == IjkVideoView.STATE_PAUSED) {
                    if (player.getParent() != null)
                        ((ViewGroup) player.getParent()).removeAllViews();
                    frameLayout.addView(player);
                    return;
                }
            }
        }

        @Override
        public void onChildViewDetachedFromWindow(View view) {
            int index = superRecyclerView.getChildAdapterPosition(view);
            if ((index) == postion) {
                if (true) {
                    if (player != null) {
                        player.stop();
                        player.release();
                        player.showView(R.id.adapter_player_control);
                    }
                }
            }
        }
    });
}

其二:重寫onConfigurationChanged

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (player != null) {
        player.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            fullScreen.setVisibility(View.GONE);
            fullScreen.removeAllViews();
            superRecyclerView.setVisibility(View.VISIBLE);
            if (postion <= mLayoutManager.findLastVisibleItemPosition()
                    && postion >= mLayoutManager.findFirstVisibleItemPosition()) {
                View view = superRecyclerView.findViewHolderForAdapterPosition(postion).itemView;
                FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.adapter_super_video);
                frameLayout.removeAllViews();
                ViewGroup last = (ViewGroup) player.getParent();//找到videoitemview的父類,然后remove
                if (last != null) {
                    last.removeAllViews();
                }
                frameLayout.addView(player);
            }
            int mShowFlags =
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            fullScreen.setSystemUiVisibility(mShowFlags);
        } else {
            ViewGroup viewGroup = (ViewGroup) player.getParent();
            if (viewGroup == null)
                return;
            viewGroup.removeAllViews();
            fullScreen.addView(player);
            fullScreen.setVisibility(View.VISIBLE);
            int mHideFlags =
                    View.SYSTEM_UI_FLAG_LOW_PROFILE
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    ;
            fullScreen.setSystemUiVisibility(mHideFlags);
        }
    } else {
        fullScreen.setVisibility(View.GONE);
    }
}

 

 

 

 

 

來自:https://github.com/supercwn/SuperPlayer

 

 本文由用戶 ldffkal 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!