为了账号安全,请及时绑定邮箱和手机立即绑定

退出 YouTube 全屏后退按钮 Cordova 关闭应用

退出 YouTube 全屏后退按钮 Cordova 关闭应用

料青山看我应如是 2021-11-12 16:06:17
我在全屏模式下播放 YouTube 视频,当我按下后退按钮时,它会关闭应用程序而不是退出全屏模式。我正在将JavaScript 与 Cordova 6.3.1 for Android 一起使用。这是我的代码:document.addEventListener("deviceready", onDeviceReady, false);function onDeviceReady() {   document.addEventListener("backbutton", function (e) {     e.preventDefault();       screen.orientation.lock('portrait');     }, false);}前面的代码没有按预期工作。提前致谢。
查看完整描述

1 回答

?
Cats萌萌

TA贡献1805条经验 获得超9个赞

我还没有找到答案,但到目前为止我正在发布我的发现,希望它可以帮助某人。


版本:cordova-android8.1.0


我发现了什么

问题来自这样一个事实,即要全屏显示 Youtube 视频,SystemWebChromeClient调用CordovaWebViewImpl.showCustomView()方法。通过这样做,新视图似乎在 Cordova 能够onDispatchKeyEvent()将backbutton事件发送到 JavaScript 端之前捕获后退按钮事件。


这是一些日志,为了清楚起见,添加了注释


// Video is fullscreen, and then dismissed with the onscreen minimize button

D/CordovaWebViewImpl: showCustomView : showing Custom View + view = android.widget.FrameLayout{a7cc344 V.E...... ......I. 0,0-0,0}

D/CordovaWebViewImpl: hideCustomView : Hiding Custom View : mCustomView = android.widget.FrameLayout{a7cc344 V.E...... .......D 0,0-1080,1704}


// While the video is not displayed fullscreen, the back button event is correctly caught and sent to the JS side

D/CordovaWebViewImpl: onDispatchKeyEvent : isBackButton = true mCustomView = null


// Once again in fullscreen, then click on the back button

D/CordovaWebViewImpl: showCustomView : showing Custom View + view = android.widget.FrameLayout{fa6d336 V.E...... ......I. 0,0-0,0}

// No back button event caught

// A destroy lifecycle event is sent (from the video FrameLayout handling?) and handleDestroy() is called

D/CordovaWebViewImpl: handleDestroy : load about:blank

D/CordovaWebViewImpl: >>> loadUrl(about:blank)

W/cr_AwContents: WebView.destroy() called while WebView is still attached to window.

D/CordovaWebViewImpl: hideCustomView : Hiding Custom View : mCustomView = android.widget.FrameLayout{a7cc344 V.E...... .......D 0,0-1080,1704}

我试过的

创建 的子类FrameLayout,并覆盖onKeyDown()和onKeyUp(),将其用于showCustomerView()

然后视频不再显示。


文档

https://developer.android.com/reference/android/widget/FrameLayout


查看完整回答
反对 回复 2021-11-12
  • 1 回答
  • 0 关注
  • 226 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号