杭漂工作日记(2)android机顶盒应用的一点心得

工作了一周的时间,适应期算是过了,总体来说新公司的节奏不是很快,但是能学到很多新东西,在职业规划上,前几年当然是要稳定基础+提高广度的。

反编译了一些机顶盒上的apk,大概明白了其设计流程,现在公司开发的是一个学前教育应用。主要功能是就是视频的播放(毕竟遥控器操作有限,很少有人拿电视做什么其他app吧)。

实现方式是android(webview+videoview)+JS(h5css3);主要是想比较容易更新界面,毕竟整个APK逻辑并不复杂,activity只需要一个,xml上只要framelayout把webview放在videoview上面就好了,这样布局还有个好处就是在js某个界面(比如一部分透明)自己定义videoview大小,窗口播放视频(如没有这个需求,再建一个VideoviewActivity更加简单)。

用到最多的就是handler,接口调用JS方法,把一些命令发给JS也通过一些回调来执行安卓端操作。比如播放视频,关闭应用,播放下一集等等。

 

整体流程为:

1.打开app,加载webview,onkeydown(),onkeyup()方法监听遥控器按键,如上下左右ok返回,发送JS指令操作H5界面;

2.选中一集视频,播放,此时发送JS指令加载透明背景,显示出位于下面的videoview加载视频url并获取焦点。onprepare方法里进行播放和进度条同步.

 

3.打开视频时 会发一条JS指令 应该是加载透明底的地址。上下有事件响应

 

当按键时 进度条可操作状态

按左右键移动进度条,同时发JS向左右的方法。视频播放时,控制当时的webview左右无响应事件。

松开后,进度条不可操作状态,视频移动到进度条所处位置播放。

按键监听:
Onkeydown()(按键按下监听)
 this.PwSeekbar.setKeyDown(true);
        switch (keyCode) {
            case KEYCODE_BACK:
                return true;
            case KEYCODE_DPAD_LEFT:
                if (Constant.type != 0) {
                    return false;
                }
                sendJsKeyPress(3);
                if (!isPrepare) {
                    return true;
                }
                Isseekto = false;
                this.PwSeekbar.setKeyDownisRight(false);
                this.PwSeekbar.keyDownIsRightMove(false);
                return true;
            case KEYCODE_DPAD_RIGHT:
                if (Constant.type != 0) {
                    return false;
                }
                sendJsKeyPress(4);
                if (!isPrepare) {
                    return true;
                }
                Isseekto = false;
                this.PwSeekbar.setKeyDownisRight(true);
                this.PwSeekbar.keyDownIsRightMove(true);
                return true;
            default:
                return false;
        }

OnkeyUp()(按键松开监听):
this.PwSeekbar.setKeyDown(false);
case KEYCODE_HOME:
            sendJsKeyPress(512);
                return true;
            case KEYCODE_BACK(返回):
            case KEYCODE_BUTTON_13:
                if (Constant.type == 0) {
                   // this.wasuTVHandler.sendEmptyMessage(10212);
                    if (this.webView.getUrl() == null) {
                        ExitApplicationDialogDialog();
                        return true;
                    } else if (this.webView.getUrl().indexOf(Constant.HomeUrl) >= 0) {
                        ExitApplicationDialogDialog();
                        return true;
                    } else if (this.openpageUrl == null || this.webView.getUrl().indexOf(this.openpageUrl) < 0) {
                        sendJsKeyPress(27);
                        return true;
                    } else {
                        this.webView.loadUrl(Constant.HomeUrl);
                        this.openpageUrl = null;
                        return true;
                    }
                }
                this.activity.showDialog(5);(弹出dialog)
                return true;
            case KEYCODE_0:
                sendJsKeyPress(48);
                return true;
            case KEYCODE_1:
                sendJsKeyPress(49);
                return true;
            case KEYCODE_2:
                sendJsKeyPress(50);
                return true;
            case KEYCODE_3:
                sendJsKeyPress(51);
                return true;
            case KEYCODE_4:
                sendJsKeyPress(52);
                return true;
            case KEYCODE_5:
                sendJsKeyPress(53);
                return true;
            case KEYCODE_6:
                sendJsKeyPress(54);
                return true;
            case KEYCODE_7:
                sendJsKeyPress(55);
                return true;
            case KEYCODE_8:
                sendJsKeyPress(56);
                return true;
            case KEYCODE_9:
                sendJsKeyPress(57);
                return true;
            case KEYCODE_DPAD_UP(上):
                if (Constant.type != 0) {
                    return false;
                }
                if (!Constant.canMove) {
                    return true;
                }
                sendJsKeyPress(1);(发JS指令,弹出选集)
                return true;
            case KEYCODE_DPAD_DOWN(下):
                if (Constant.type != 0) {
                    return false;
                }
                if (!Constant.canMove) {
                    return true;
                }
                sendJsKeyPress(2);
                return true;
            case KEYCODE_DPAD_LEFT(左):
                if (Constant.type != 0) {
                    return false;
                }
                if (!isPrepare) {
                    return true;
                }
                this.PwSeekbar.VideoViewSeekTo();(让视频在进度条停止的地方开始播放)
                Isseekto = true;
                return true;
            case KEYCODE_DPAD_RIGHT(右):
                if (Constant.type != 0) {
                    return false;
                }
                if (!isPrepare) {
                    return true;
                }
                this.PwSeekbar.VideoViewSeekTo();
                Isseekto = true;
                return true;
            case KEYCODE_DPAD_CENTER(大OK):
            case KEYCODE_ENTER(确认):
                if (Constant.type != 0) {
                    return false;
                }
                if (isPrepare) {
                    this.wasuTVHandler.sendEmptyMessage(10212);
                }
                this.lastUrl = this.webView.getUrl();
                sendJsKeyPress(13);
                return true;
            case KEYCODE_VOLUME_UP:
                sendJsKeyPress(101);
                return true;
            case KEYCODE_VOLUME_DOWN:
                sendJsKeyPress(102);
                return true;
            case KEYCODE_POWER:
                sendJsKeyPress(ConstantBase.KEYCODE_power);
                return true;
            case KEYCODE_DEL:
                sendJsKeyPress(ConstantBase.keyCODE_del);
                return true;
            case KEYCODE_MENU:
                if (!ContinuationPressKey.pressKey(8)) {
                    return true;
                }
                startActivity(new Intent().setClass(this, DisplayActivity.class));
                return true;
            case KEYCODE_MEDIA_NEXT:
                sendJsKeyPress(ConstantBase.KEYCODE_MEDIA_next);
                return true;
            case KEYCODE_MEDIA_PREVIOUS:
                sendJsKeyPress(ConstantBase.KEYCODE_MEDIA_previous);
                return true;
            case KEYCODE_F1:
                sendJsKeyPress(ConstantBase.KEYCODE_f1);
                return true;
            case KEYCODE_F2:
                sendJsKeyPress(ConstantBase.KEYCODE_f2);
                return true;
            case KEYCODE_F3:
                sendJsKeyPress(ConstantBase.KEYCODE_f3);
                return true;
            case KEYCODE_F4:
                sendJsKeyPress(ConstantBase.KEYCODE_f4);
                return true;
            case KEYCODE_APP_SWITCH:
                sendJsKeyPress(513);
                return true;
            case KEYCODE_3D_MODE:
                sendJsKeyPress(500);
                return true;
            case 10011:
                sendJsKeyPress(ConstantBase.KEYCODE_tvguide);
                return true;

 private void sendJsKeyPress(final int code) {
        Log.i("WelcomeActivity", "keyCode = " + code);
        Log.i("WelcomeActivity", "canMove = " + Constant.canMove);
        Handler handler = new Handler();
        if (Constant.canMove) {
            handler.post(new Runnable() {
                public void run() {
                    if (WelcomeActivity.this.shouldSendJs) {
                        WelcomeActivity.this.webView.loadUrl("javascript:" + ("var event = document.createEvent('Events');event.initEvent('keydown',true,true);event.view = document.defaultView;event.altKey = false;event.ctrlKey = false;event.shiftKey = false;event.metaKey = false;event.keyCode = " + code + ";event.charCode = " + code + ";event.which = " + code + ";document.body.dispatchEvent(event);"));
                        WelcomeActivity.this.shouldSendJs = false;
                        WelcomeActivity.this.mHandler.postDelayed(new Runnable() {
                            public void run() {
                                WelcomeActivity.this.shouldSendJs = true;
                            }
                        }, 100);
                    }
                }
            });
        }
}

public void open(Message msg) {
        this.wasuTVHandler.sendEmptyMessage(1002);
        this.URL = msg.getData().getString("URL");
        String mediatype = msg.getData().getString("mediatype");
        String mode = msg.getData().getString("mode");
        Log.e("welcome ", "open URL: " + this.URL);
        this.videoView.setVideoPath(this.URL);
    }

case 1002:
                    WelcomeActivity.this.webView.loadUrl("javascript:handleMediaMessage('2')");
                    WelcomeActivity.this.startUpdateRate();

 

印象比较深的大概就是这么多。每天进步一点点。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值