Check if a WebView is playing a video

前端 未结 1 1305
甜味超标
甜味超标 2020-12-18 14:21

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?

I already have tried this

1条回答
  •  一生所求
    2020-12-18 15:14

    You can detect whether video is playing using JavaScript.

    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.
      });
      
    2. 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)" +
                  "})()")
      

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

    3. 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
         }
       }
      }
      

    Set webView use JavaScriptInterface.

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

    And then call.

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

    I hope it can help you.

    0 讨论(0)
提交回复
热议问题