3

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 code:

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();

            }
        }

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?

Nils Be
  • 45
  • 1
  • 4

1 Answers1

3

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mel
  • 199
  • 1
  • 10
  • Thank you for your awnser. But I still have some problems: I can't use `@JavaScriptInterface` because: Android Studio says: Annotiations are not allowed here. And I don't know how to use the JavaScript (because it's the first time I use it) When I copy this code I only get errors. (E.g. `view?.context?.assets?.let {` And what does val mean? Sorry for my stupid questions but I'm an beginner in programming... – Nils Be Jul 12 '18 at 15:18
  • @NilsBe See this. [How to add JavaScriptInterface](https://developer.android.com/reference/android/webkit/WebView#addJavascriptInterface(java.lang.Object,%20java.lang.String)) And `val` is used in `Kotlin`. Check number 2 in my answer. I linked a url for same code in `Java` version. – mel Jul 16 '18 at 04:35
  • Thank you very much, no it works, but is there also any possibillity to get the direct url of the video that is currently playing? – Nils Be Jul 16 '18 at 14:53
  • @NilsBe I've heard there's a way to get video url, using `JavaScript` But I'd never tried. How about this? Have a one flag (like boolean type variable that expresses whether video is playing or not) and in `onLoadResources` method, get video url only this flag is false (video is not playing). – mel Jul 17 '18 at 00:28
  • Hello @mel.j, I have followed your solution but it doesn't work. Could you please give me full sample? Thanks in advanced! – Na Pro Jun 08 '21 at 09:33