Android机顶盒开发中的视频播放快退快进问题的处理

       在Android手机和平板的软件开发中,我们开发视频时,可以用很多的第三方视频播放库进行集成,这些库都是比较完善的。但是,我们在机顶盒上进行视频开发时,发现网上很少有完美适配视频快进快退的问题的,这让我们处理机顶盒视频播放时的快退快进问题时,会很麻烦,只能自己自己处理他们的快进快退问题。今天就以GSYVideoPlayer为例,给大家详细讲解一下如何处理快进快退问题。

       首先,导入依赖

implementation 'com.shuyu:GSYVideoPlayer:7.1.6'
自定义CustomStandardGSYVideoPlayer继承StandardGSYVideoPlayer,然后我们在Activity中监听onKeyDown方法,然后利用Handler发送按键消息对快进快退进行自定义处理,如下代码所示
package cmcc.chinamobile.tv.cn.widget;

import static com.shuyu.gsyvideoplayer.utils.CommonUtil.hideNavKey;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.NonNull;

import com.shuyu.gsyvideoplayer.utils.CommonUtil;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.lang.ref.WeakReference;

import cmcc.chinamobile.tv.cn.R;

public class CustomStandardGSYVideoPlayer extends StandardGSYVideoPlayer {

    private MyHandler mHandler;

    public CustomStandardGSYVideoPlayer(Context context, Boolean fullFlag) {
        super(context, fullFlag);
    }

    public CustomStandardGSYVideoPlayer(Context context) {
        super(context);
    }

    public CustomStandardGSYVideoPlayer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void init(Context context) {
        super.init(context);
        mHandler = new MyHandler(this);
        findViewById(R.id.rl_start).setOnClickListener(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.custom_video_layout_standard;
    }



    //在某些不能用ennter键的情况下进行测试
    @Override
    public void onClick(View v) {
        super.onClick(v);
        int i = v.getId();
        if (mHideKey && mIfCurrentIsFullscreen) {
            hideNavKey(mContext);
        }
        if (i == R.id.rl_start) {
            clickStartIcon();
        }
    }

    @Override
    protected boolean isShowNetConfirm() {
        return false;
    }

    //处理按键快进和快退
    //连续按下左右键
    private void keyMove() {
        if (mSeekTimePosition >= getDuration()) {
            mSeekTimePosition = getDuration();
            cancelProgressTimer();
            if (mProgressBar != null) {
                mProgressBar.setProgress(100);
            }
            if (mCurrentTimeTextView != null && mTotalTimeTextView != null) {
                mCurrentTimeTextView.setText(mTotalTimeTextView.getText());
            }
            if (mBottomProgressBar != null) {
                mBottomProgressBar.setProgress(100);
            }
            findViewById(R.id.thumb).setVisibility(View.VISIBLE);
            mBottomContainer.setVisibility(GONE);
        } else {
            mBottomContainer.setVisibility(VISIBLE);
            long progress = mSeekTimePosition * 100 / (getDuration() == 0 ? 1 : getDuration());
            if (mProgressBar != null)
                mProgressBar.setProgress((int) progress);

//            if (mCurrentTimeTextView != null) {
//                mCurrentTimeTextView.setText(CommonUtil.stringForTime(progress * getDuration() / 100));
//            }
            if (mBottomProgressBar != null) {
                mBottomProgressBar.setProgress((int) progress);
            }

            getGSYVideoManager().seekTo(mSeekTimePosition);
            if (System.currentTimeMillis()-lastTime>1000){
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mBottomContainer.setVisibility(GONE);
                    }
                }, 500);
            }
            lastTime=System.currentTimeMillis();
            Log.d("mz--", "keyMove:" + mSeekTimePosition + ":" + getDuration() + ":" + mSeekTimePosition * 100 / getDuration());
        }
    }

    //定义变量
    private static final int LEFT = 0;
    private static final int RIGHT = 1;
    private static final int CANCLE = 2;
    // TODO: 2023/2/23 判断是否连续点击快进键,避免控件频繁的visible和gone
    private static  long lastTime=0;


    //程序启动时,初始化并发送消息
    // TODO: 2022/12/20 用 CustomStandardGSYVideoPlayer的弱引用,防止内存泄泄露
    private static class MyHandler extends Handler {
        private WeakReference<CustomStandardGSYVideoPlayer> weakReference;

        public MyHandler(CustomStandardGSYVideoPlayer videoPlayer) {
            weakReference = new WeakReference<>(videoPlayer);
        }

        @Override
        public void handleMessage(Message msg) {
            CustomStandardGSYVideoPlayer videoPlayer = weakReference.get();
            videoPlayer.mSeekTimePosition= (int) videoPlayer.getGSYVideoManager().getCurrentPosition();
            if (System.currentTimeMillis()-lastTime>1000){
                videoPlayer.mBottomContainer.setVisibility(VISIBLE);
            }
            switch (msg.what) {
                case LEFT:
                    if (videoPlayer.mSeekTimePosition > 2000) {
                        videoPlayer.mSeekTimePosition -= 2000;
                    } else {
                        videoPlayer.mSeekTimePosition = 0;
                    }
                    videoPlayer.keyMove();
                    break;
                case RIGHT:
                    if (videoPlayer.mSeekTimePosition < videoPlayer.getDuration()) {
                        videoPlayer.mSeekTimePosition += 2000;
                    } else {
                        videoPlayer.mCurrentState = CURRENT_STATE_AUTO_COMPLETE;
                        videoPlayer.mSeekTimePosition = videoPlayer.getDuration();
                        videoPlayer.cancelProgressTimer();
                        if (videoPlayer.mProgressBar != null) {
                            videoPlayer.mProgressBar.setProgress(100);
                        }
                        if (videoPlayer.mCurrentTimeTextView != null && videoPlayer.mTotalTimeTextView != null) {
                            videoPlayer.mCurrentTimeTextView.setText(videoPlayer.mTotalTimeTextView.getText());
                        }
                        if (videoPlayer.mBottomProgressBar != null) {
                            videoPlayer.mBottomProgressBar.setProgress(100);
                        }

                        videoPlayer.changeUiToCompleteShow();
                        videoPlayer.cancelDismissControlViewTimer();

                        videoPlayer.findViewById(R.id.thumb).setVisibility(View.VISIBLE);
                        videoPlayer.mBottomContainer.setVisibility(GONE);
                        return;
                    }
                    videoPlayer.keyMove();
                    break;
                case CANCLE:                        //停止按键
                    videoPlayer.onStopTrackingTouch(videoPlayer.mProgressBar);
                    videoPlayer.mBottomContainer.setVisibility(GONE);
                    videoPlayer.startDismissControlViewTimer();
                    videoPlayer.touchSurfaceUp();
                    videoPlayer.startProgressTimer();
                    //不要和隐藏虚拟按键后,滑出虚拟按键冲突
                    if (videoPlayer.mHideKey && videoPlayer.mShowVKey) {
                        // return true;
                    }
                    break;
            }
        }
    }

    @Override
    protected void dismissProgressDialog() {
        try {
            if (mProgressDialog != null) {
                mProgressDialog.dismiss();
                mProgressDialog = null;
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setOnkeyDown(int keyCode, KeyEvent event) {
        Log.d("mz--", "setOnkeyDown: " + keyCode);
        switch (keyCode) {
            case KeyEvent.ACTION_DOWN:
                break;
            case KeyEvent.ACTION_UP:
                break;
            case KeyEvent.KEYCODE_DPAD_LEFT:
                mHandler.sendEmptyMessage(LEFT);
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                mHandler.sendEmptyMessage(RIGHT);
                break;
            case KeyEvent.KEYCODE_DPAD_CENTER:
            case KeyEvent.KEYCODE_ENTER:
            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                if (mSeekTimePosition >= getDuration()) {
                    mSeekTimePosition = 0;
                }
                Log.d("mz--", "mSeekTimePosition: " + mSeekTimePosition);
                clickStartIcon();
                break;
            default:
                break;
        }
    }

    @Override
    public void onAutoCompletion() {
        super.onAutoCompletion();
        mSeekTimePosition = 0;
    }
}
setOnkeyDown方法监听按键位置,MyHandler发送按键消息,keyMove方法对视频快进快退进行机顶盒的适配处理

demo链接:https://download.youkuaiyun.com/download/weixin_41208172/87815354?spm=1001.2014.3001.5503

 
 
 
 

                
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值