Reputation: 45
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?
Upvotes: 3
Views: 4103
Reputation: 199
You can detect whether video is playing using JavaScript.
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.
});
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
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.
Upvotes: 3