检查WebView是否正在播放视频 [英] Check if a WebView is playing a video

查看:183
本文介绍了检查WebView是否正在播放视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以检查WebView当前是否正在播放视频,如果可以,则可以下载该视频的http:// ... .mp4 / URL?

Is it possible to check if a WebView is playing a Video at the moment and if yes to get the http://... .mp4/ URL of this Video to download?

我已经尝试过此代码:

public void onLoadResource(WebView view, final String url){
            super.onLoadResource(view, url);
            if(url.endsWith(".mp4") & !url.contains(".jpg") & !url.contains(".png")){

                AlertDialog.Builder video_downloaden = new AlertDialog.Builder(SearchActivity.this);
                video_downloaden.setTitle("Video downloaden?");
                video_downloaden.setMessage("Download Video?");
                video_downloaden.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        DownloadManager.Request request_video = new DownloadManager.Request(Uri.parse(url));
                        request_video.allowScanningByMediaScanner();

                        request_video.setVisibleInDownloadsUi(false);

                        DownloadManager downloadManager2 = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);

                        request_video.setDestinationInExternalFilesDir(getApplicationContext(), DIRECTORY_DOWNLOADS, File.separator + ".Videos" + File.separator + "video" + video_number);

                        downloadManager2.enqueue(request_video);
                        Toast.makeText(SearchActivity.this,"Download started",Toast.LENGTH_LONG).show();

                    }
                });
                video_downloaden.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.cancel();
                    }
                });

                video_downloaden.show();

            }
        }

onLoadResource()始终显示AlertDialog,并且每次都无法下载...
所以我的问题是,是否还有其他机会检查WebView是否正在播放视频并获取视频URL?

But with onLoadResource() the AlertDialog is shown all the time and the download doesn't work every time... So my question is, if there is any other opportunity to check if the WebView is playing a Video and to get the Video url?

推荐答案

您可以检测视频是否正在使用JavaScript播放。

You can detect whether video is playing using JavaScript.


  1. 资产中创建 .js 文件目录,并声明JavaScript函数和jQuery,如下所示:

  1. Create a .js file in the assets directory and declare a JavaScript function and jQuery like this:

$("video").on("play", function() {
 //You can call JavaScriptInterface here.
});


  • 插入此 .js 文件在包含视频的网页中。当调用 onPageFinished 时,我插入了 .js 文件。

  • Insert this .js file in web page that contains video. I inserted .js file when onPageFinished is called.

    view?.context?.assets?.let {
        val inputStream: InputStream = it.open(scriptFile)
        val buffer = ByteArray(inputStream.available())
        inputStream.read(buffer)
        inputStream.close()
    
        val encoded = Base64.encodeToString(buffer, Base64.NO_WRAP)
    
        view.loadUrl("javascript:(function() {" +
                "var parent = document.getElementsByTagName('head').item(0);" +
                "var script = document.createElement('script');" +
                "script.type = 'text/javascript';" +
                "script.innerHTML = window.atob('$encoded');" +
                "parent.appendChild(script)" +
                "})()")
    

    如果不熟悉Kotlin的人,请查看以下参考: An droid Web-View:将本地Javascript文件注入到远程网页

    If you are not familiar with Kotlin, check this reference: Android Web-View : Inject local Javascript file to Remote Webpage

    完成!如果使用 JavaScriptInterface ,则可以执行所需的任何操作。

    Done! If you use JavaScriptInterface, you can do whatever you want, like this.

    class WebScriptInterface(val button: View) {
    
     @JavascriptInterface
     fun hideFloatingButton() {
       if (button.visibility == View.VISIBLE) {
        button.visibility = View.GONE
       }
     }
    }
    


  • 设置 webView 使用 JavaScriptInterface

    webView.addJavascriptInterface
     (WebScriptInterface(binding.floatBrowserCollect), "App")
    

    然后致电。

     $("video").on("play", function() {
        App.hideFloatingButton()
      });
    

    我希望它能为您提供帮助。

    I hope it can help you.

    这篇关于检查WebView是否正在播放视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆