Javascript 如何检测Zidoo Digital Android Box连接或断开的HDMI状态?

Javascript 如何检测Zidoo Digital Android Box连接或断开的HDMI状态?,javascript,java,android,android-studio,hdmi,Javascript,Java,Android,Android Studio,Hdmi,我正在与ziddo digital box合作,用于视频播放中的HDMI。连接和断开HDMI时出现问题。屏幕冻结了。 我正在使用以下代码: public class RealtekSurfaceView extends SurfaceView { public static final String TAG = "RealtekSurfaceView"; //A reference to the context private Context mContext = null; //Refe

我正在与ziddo digital box合作,用于视频播放中的HDMI。连接和断开HDMI时出现问题。屏幕冻结了。 我正在使用以下代码:

public class RealtekSurfaceView extends SurfaceView {

public static final String TAG = "RealtekSurfaceView";

//A reference to the context
private Context mContext = null;

//Reference to ViewGroup
private ViewGroup mViewGroup = null;

//Integers to keep the width, height and FPS
private int width = 0;
private int height = 0;
private int fps = 0;

//Reference to the Surface holder
private SurfaceHolder mSurfaceHolder = null;
private SurfaceHolder.Callback mSurfaceHolderCallback = null;

//Reference to the Broadcast receiver
private BroadcastReceiver mHdmiRxReceiver = null;

//Boolean to indicate if the HDMI IN is connected
private boolean isConnected = false;

//A few constants to pass the data along
public static final int DISPLAY = 0;
public static final int DISPLAYTIME = 200;

//A reference to a handler
Handler mHandler = null;

//Reference to realtek hdmi manager
private RtkHDMIRxManager mRtkHdmiManager = null;

//Boolean to identify if the display is ON or OFF
public static boolean isPreviewAvailable = false;

//A boolean to denote if we are displaying already
private boolean isDisplayOn = false;

String videoPath;

String videoType;

boolean HDMI_AV=false;




//Constructor to the RealtekSurfaceView class, this will take care of populating the context, view group, surface holder.
//It will also take care of setting up the surfaceholder callback
public RealtekSurfaceView(Context ctx, ViewGroup viewGroup) {
    super(ctx);
    this.mContext = ctx;
    this.mViewGroup = viewGroup;
    this.mSurfaceHolder = getHolder();
    //Add the callback
    mSurfaceHolderCallback = new SurfaceCallback();
    mSurfaceHolder.addCallback(mSurfaceHolderCallback);
    init();
}

public RealtekSurfaceView(Context context,String filePath,String videoType,int x,int y,int width,int height,boolean HDMI_AV){
    super(context);
    // TODO Auto-generated constructor stub
    this.mContext=context;
    this.videoType=videoType;
    this.width=width;
    this.height=height;
    this.HDMI_AV=HDMI_AV;
    this.videoPath=filePath;
    this.mSurfaceHolder = getHolder();
    this.mSurfaceHolder.addCallback(new SurfaceCallback());
    this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    setBackgroundColor(Color.TRANSPARENT);
}

public void init() {
    //Make the handler instance ready to be used by remaining components
    mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
        switch (msg.what) {
            case DISPLAY:
                if (isConnected) {
                    play();
                }
            break;

            default:
            break;
        }
        }
    };
    initHdmiConnect();
}

//Start listening to and processing the incoming HDMI IN connection from the Realtek internal classes
private void initHdmiConnect() {
    mHdmiRxReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            isConnected = intent.getBooleanExtra(HDMIRxStatus.EXTRA_HDMIRX_PLUGGED_STATE, false);
            if (isConnected) {
                play();
            } else {
                stop();
            }
        }
    };
    isConnected = isHdmiConnected(mContext);
    mContext.registerReceiver(mHdmiRxReceiver, new IntentFilter(HDMIRxStatus.ACTION_HDMIRX_PLUGGED));
}

//Function to determine if the HDMI IN is connected
public static boolean isHdmiConnected(Context ctx) {
    Intent batteryStatus = ctx.registerReceiver(null, new IntentFilter(HDMIRxStatus.ACTION_HDMIRX_PLUGGED));
    if(null != batteryStatus) {
        return batteryStatus.getBooleanExtra(HDMIRxStatus.EXTRA_HDMIRX_PLUGGED_STATE, false);
    } else {
        return false;
    }
}

//The surfaceholder.callback implementation, this will try to launch/stop the play by appropriately setting up few variables
private final class SurfaceCallback implements SurfaceHolder.Callback {
    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
        Log.i("RealtekSurface", "changed");
    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        isPreviewAvailable = true;
        play();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        isPreviewAvailable = false;
        stop();
    }
}

public boolean play() {
    this.setVisibility(View.VISIBLE);
    if (!isDisplayOn && isPreviewAvailable) {
        mRtkHdmiManager = new RtkHDMIRxManager();
        if (null != mRtkHdmiManager) {
            HDMIRxStatus mHdmiRxStatus = mRtkHdmiManager.getHDMIRxStatus();
            if (mHdmiRxStatus != null && mHdmiRxStatus.status == HDMIRxStatus.STATUS_READY) {
                if (mRtkHdmiManager.open() != 0) {
                    //TODO write code for retrying the same after "t" time using the handler
                    height = 0;
                    width = 0;
                    mRtkHdmiManager = null;
                    mHandler.sendEmptyMessageDelayed(DISPLAY, DISPLAYTIME);
                    return false;
                }
                HDMIRxParameters mHdmiRxParams = mRtkHdmiManager.getParameters();
                getSupportedPreviewSizes(mHdmiRxParams, mHdmiRxStatus.width, mHdmiRxStatus.height);
                fps = getSupportedFps(mHdmiRxParams);
            } else {
                if(null!=mHandler)
                mHandler.sendEmptyMessageDelayed(DISPLAY, DISPLAYTIME);
                return false;
            }
            //Following is the code to play
            try {
                mRtkHdmiManager.setPreviewDisplay(mSurfaceHolder);
                HDMIRxParameters mParams = new HDMIRxParameters();
                mParams.setPreviewSize(width, height);
                mParams.setPreviewFrameRate(fps);
                mRtkHdmiManager.setParameters(mParams);
                mRtkHdmiManager.play();
                isDisplayOn = true;
            } catch (Exception e) {
                stop();
                e.printStackTrace();
            }
        }
    } else if (!isPreviewAvailable) {
        //TODO write code for retrying the same after "t" time using the handler
        if(null!=mHandler)
        mHandler.sendEmptyMessageDelayed(DISPLAY, DISPLAYTIME);
        return false;
    } else {
        return false;
    }
    return true;
}

private void stop() {
    //Reset the visibility of the surface view to invisible, else the HDMI IN will not be able to retrieve this view
    this.setVisibility(View.INVISIBLE);
    //Destroy the instance of the running RealTek Manager after stopping that
    if (mRtkHdmiManager != null) {
        mRtkHdmiManager.stop();
        mRtkHdmiManager.release();
        mRtkHdmiManager = null;
    }
    //Reset the booleans to their origin values
    isDisplayOn = false;
    width = 0;
    height = 0;
    fps = 0;
}

private int getSupportedFps(HDMIRxParameters mHdmiRxParameters) {
    List<Integer> previewFrameRates = mHdmiRxParameters.getSupportedPreviewFrameRates();
    int fps = 0;
    if (previewFrameRates != null && previewFrameRates.size() > 0) {
        fps = previewFrameRates.get(previewFrameRates.size() - 1);
    } else {
        fps = 30;
    }
    return fps;
}

private void getSupportedPreviewSizes(HDMIRxParameters mHdmiRxParams, int rxWidth, int rxHeight) {
    List<RtkHDMIRxManager.Size> mPreviewSizes = mHdmiRxParams.getSupportedPreviewSizes();
    int retWidth = 0, retHeight = 0;
    if (mPreviewSizes == null || mPreviewSizes.size() <= 0) {
        return;
    }
    for (int i = 0; i < mPreviewSizes.size(); i++) {
        if (mPreviewSizes.get(i) != null && rxWidth == mPreviewSizes.get(i).width) {
            retWidth = mPreviewSizes.get(i).width;
            retHeight = mPreviewSizes.get(i).height;
            if (rxHeight == mPreviewSizes.get(i).height) {
                break;
            }
        }
    }

    if (retWidth == 0 && retHeight == 0) {
        if (mPreviewSizes.get(mPreviewSizes.size() - 1) != null) {
            width = mPreviewSizes.get(mPreviewSizes.size() - 1).width;
            height = mPreviewSizes.get(mPreviewSizes.size() - 1).height;
        }
    }

    width = retWidth;
    height = retHeight;
}
公共类RealtekSurfaceView扩展了SurfaceView{
公共静态最终字符串TAG=“RealtekSurfaceView”;
//对上下文的引用
私有上下文mContext=null;
//对视图组的引用
私有视图组mViewGroup=null;
//保持宽度、高度和FPS的整数
私有整数宽度=0;
私有整数高度=0;
私有整数fps=0;
//参考表面保持架
私有SurfaceHolder mSurfaceHolder=null;
private SurfaceHolder.Callback mSurfaceHolderCallback=null;
//对广播接收机的引用
专用广播接收器mHdmiRxReceiver=null;
//布尔值,指示HDMI IN是否已连接
私有布尔值isConnected=false;
//传递数据的几个常量
公共静态最终整数显示=0;
公共静态最终整数显示时间=200;
//对处理程序的引用
Handler mHandler=null;
//参考realtek hdmi管理器
专用RtkHDMIRxManager mRtkHdmiManager=null;
//布尔值,用于标识显示是打开还是关闭
公共静态布尔值isPreviewAvailable=false;
//表示是否已显示的布尔值
私有布尔ISDISPLAYN=false;
字符串视频路径;
字符串视频类型;
布尔HDMI_AV=假;
//构造函数,这将负责填充上下文、视图组和曲面持有者。
//它还将负责设置surfaceholder回调
公共RealtekSurfaceView(上下文ctx、视图组视图组){
超级(ctx);
this.mContext=ctx;
this.mViewGroup=视图组;
this.mSurfaceHolder=getHolder();
//添加回调
mSurfaceHolderCallback=新的SurfaceCallback();
mSurfaceHolder.addCallback(mSurfaceHolderCallback);
init();
}
公共RealtekSurfaceView(上下文上下文、字符串文件路径、字符串视频类型、整数x、整数y、整数宽度、整数高度、布尔HDMI_AV){
超级(上下文);
//TODO自动生成的构造函数存根
this.mContext=上下文;
this.videoType=videoType;
这个。宽度=宽度;
这个。高度=高度;
这个.HDMI_AV=HDMI_AV;
this.videoPath=filePath;
this.mSurfaceHolder=getHolder();
this.mSurfaceHolder.addCallback(新的SurfaceCallback());
this.mSurfaceHolder.setType(SurfaceHolder.SURFACE\u TYPE\u PUSH\u BUFFERS);
setBackgroundColor(颜色:透明);
}
公共void init(){
//使处理程序实例准备好供其余组件使用
mHandler=新处理程序(){
@凌驾
公共无效handleMessage(消息消息消息){
开关(msg.what){
案例展示:
如果(未连接){
play();
}
打破
违约:
打破
}
}
};
initHdmiConnect();
}
//开始收听和处理Realtek内部类中连接的传入HDMI
私有void initHdmiConnect(){
mHdmiRxReceiver=新广播接收器(){
@凌驾
公共void onReceive(上下文、意图){
isConnected=intent.getBooleanExtra(HDMIRxStatus.EXTRA_HDMIRX_plucked_状态,false);
如果(未连接){
play();
}否则{
停止();
}
}
};
isConnected=ishdimconnected(mContext);
mContext.registerReceiver(mHdmiRxReceiver,新的IntentFilter(HDMIRxStatus.ACTION_HDMIRX_));
}
//用于确定HDMI IN是否已连接的功能
公共静态连接(上下文ctx){
Intent batteryStatus=ctx.registerReceiver(null,新IntentFilter(HDMIRxStatus.ACTION_HDMIRX_));
如果(空!=电池状态){
返回batteryStatus.getBooleanExtra(HDMIRxStatus.EXTRA_HDMIRX_plucked_状态,false);
}否则{
返回false;
}
}
//在surfaceholder.callback实现中,这将通过适当设置几个变量来尝试启动/停止播放
私有最终类SurfaceCallback实现SurfaceHolder.Callback{
@凌驾
公共无效表面更改(表面更改表面更改表面更改,int i,int i1,int i2){
Log.i(“RealtekSurface”、“变更”);
}
@凌驾
已创建的公共空隙表面(表面层表面层){
isPreviewAvailable=真;
play();
}
@凌驾
公共空间表面已覆盖(表面层表面层){
isPreviewAvailable=假;
停止();
}
}
公共布尔播放(){
此.setVisibility(View.VISIBLE);
如果(!isDisplay&&isPreviewAvailable){
mrtkhdmimager=新的RtkHDMIRxManager();
if(null!=mrtkhdmimager){
HDMIRxStatus mHdmiRxStatus=mrtkhdmimager.getHDMIRxStatus();
if(mHdmiRxStatus!=null&&mHdmiRxStatus.status==HDMIRxStatus.status_READY){
如果(mrtkhdmimager.open()!=0){
//TODO编写代码,以便在“t”时间后使用处理程序重试相同的操作
高度=0;
宽度=0;
mrtkhdmimager=null;
mHandler.sendEmptyMessageDelayed(显示,显示时间);
返回false;
}
HDMIRxParameters mHdmiRxParams=mrtkhdmimager.getParameters();
getSupportedPreviewSizes(mHdmiRxParams、mHdmiRxStatus.width、mHdmiRxStatus.height);
fps=getSupportedFps(mHdmiRxParams);
}否则{
if(null!=mHandler)
mHandler.sendEmptyMessageDelayed(显示,显示时间);
返回false;
}
//下面是要播放的代码
试一试{
mrtkhdmimager.setPreviewDisplay(mSurfaceHolder);
HDMIRxParameters mParams=新的HDMIRxParameters();
mParams.setPreviewSize(宽度、高度);
public RealtekSurfaceView(Context context,String filePath,String videoType,int x,int y,int width,int height,boolean HDMI_AV){
super(context);
  this.mContext=context;
  this.videoType=videoType;
  this.width=width;
  this.height=height;
  this.HDMI_AV=HDMI_AV;
  this.videoPath=filePath;
  this.mSurfaceHolder = getHolder();
  this.mSurfaceHolder.addCallback(new SurfaceCallback());
  this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  setBackgroundColor(Color.TRANSPARENT);

init(); <-- you have to add this, as you are forgetting to initialize the broadcast receiver