我有一个网络视图,可以播放iframe视频,我想知道用户何时播放或暂停视频。我试过从我的java代码中使用javascriptinterface,但它不起作用。这是我的代码库:
//Javascript function
private void loadPlayJs(WebView webView) {
webView.loadUrl(
"javascript:(function f() { \n" +
"var btns = document.getElementsByTagName('button'); \n"+
"for (var i = 0, n = btns.length; i < n; i++) {\n" +
"if (btns[i].getAttribute('class') === 'play ') {\n"+
"btns[i].setAttribute('onclick', 'Android.onClicked()');"+
"}\n"+
"else{\n"+
"btns[i].setAttribute('onclick', 'Android.onClicked()');\n"+
"}\n"+
"}\n"+
"})()"
);
}
//Javascript Interface Class
class WebViewInterface{
@JavascriptInterface
public void onClicked(){
Toast.makeText(LessonActivity.this, "button clicked", Toast.LENGTH_SHORT).show();
Log.d("Java Script", "Clicked already");
}
}
//WebView settings method
private void playVimeoOnWebView(String videoUrl) {
vimeoWebViewPlayer = findViewById(R.id.videoPlayerWebView);
WebViewClient webViewClient = new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
Log.d("onPage Finished", "Finished Loading page");
loadPlayJs(view);
}
};
vimeoWebViewPlayer.getSettings().setJavaScriptEnabled(true); // enable javascript
vimeoWebViewPlayer.getSettings().getAllowContentAccess();
vimeoWebViewPlayer.getSettings().setDomStorageEnabled(true);
vimeoWebViewPlayer.getSettings().setAppCacheEnabled(true);
vimeoWebViewPlayer.getSettings().setAppCachePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/cache");
vimeoWebViewPlayer.getSettings().setDatabaseEnabled(true);
vimeoWebViewPlayer.getSettings().setDatabasePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/databases");
vimeoWebViewPlayer.getSettings().setPluginState(WebSettings.PluginState.ON);
vimeoWebViewPlayer.getSettings().setDomStorageEnabled(true);
vimeoWebViewPlayer.setWebChromeClient(new WebChromeClient());
vimeoWebViewPlayer.setWebViewClient(webViewClient);
//add the javascript interface
vimeoWebViewPlayer.addJavascriptInterface(new WebViewInterface(), "Android");
vimeoWebViewPlayer.loadUrl(videoUrl);
<!-- HTML Button I am trying to access -->
<button type="button" class="play rounded-box state-paused">
<div class="tiny-bars">
<svg width="100%" height="100%" viewBox="0 0 65 40" focusable="false">
<defs><clipPath id="rounded-border"><rect height="100%" width="100%" x="0" y="0" rx="5"></rect></clipPath> <pattern id="tiny-buffer" patternUnits="userSpaceOnUse" x="0" y="0" width="10" height="10" viewBox="0 0 10 10"><line x1="5" y1="-1" x2="-5" y2="10" stroke-width="2" stroke="#666" stroke-linecap="butt"></line><line x1="10" y1="-1" x2="0" y2="10" stroke-width="2" stroke="#666" stroke-linecap="butt"></line><line x1="15" y1="-1" x2="5" y2="10" stroke-width="2" stroke="#666" stroke-linecap="butt"></line></pattern> </defs><g clip-path="url(#rounded-border)"><rect class="buffer hidden" height="3" width="110%" x="0" y="37" fill="url(#tiny-buffer)"></rect><rect class="loaded" height="3" width="0" x="0" y="37" fill="#666"></rect><rect class="played fill" height="3" width="0" x="0" y="37"></rect></g></svg></div><div class="play-icon"><svg viewBox="0 0 20 20" preserveAspectRatio="xMidYMid" focusable="false" aria-labelledby="play-icon-title" role="img"><title id="play-icon-title">Play</title><polygon class="fill" points="1,0 20,10 1,20"></polygon></svg></div><div class="pause-icon"><svg viewBox="0 0 20 20" preserveAspectRatio="xMidYMid" focusable="false" aria-labelledby="pause-icon-title" role="img"><title id="pause-icon-title">Pause</title><rect class="fill" width="6" height="20" x="0" y="0"></rect><rect class="fill" width="6" height="20" x="12" y="0"></rect></svg></div><div class="replay-icon"><svg viewBox="-387 605 16 16" preserveAspectRatio="xMidYMid" aria-labelledby="replay-icon-title" role="img"><title id="replay-icon-title">Play</title><path class="fill" d="M-387 606v4c0 .6.4 1 1 1h4c.6 0 1-.4 1-1s-.4-1-1-1h-1.5c1.1-1.2 2.7-2 4.5-2 3.3 0 6 2.7 6 6s-2.7 6-6 6c-2.3 0-4.4-1.3-5.4-3.4-.2-.5-.8-.7-1.3-.5-.5.2-.7.8-.5 1.3 1.3 2.8 4.2 4.6 7.2 4.6 4.4 0 8-3.6 8-8s-3.6-8-8-8c-2.3 0-4.5 1-6 2.7V606c0-.6-.4-1-1-1s-1 .4-1 1z"></path></svg></div></button>
问题是,当我点击播放按钮时,我没有收到logcat消息。谢谢
暂无答案!
目前还没有任何答案,快来回答吧!