Reputation: 2533
I'm using WebView
in application for load urls. The functionality which I want, is detect video & get url if any videos is playing in the webview.
I already used WebViewClient
and shouldOverrideUrlLoading(WebView view, String url)
Webview Code
webview = findViewById(R.id.webview);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webview.getSettings().setPluginState(WebSettings.PluginState.ON);
webview.getSettings().setBuiltInZoomControls(true);
webview.getSettings().setDisplayZoomControls(true);
webview.getSettings().setDomStorageEnabled(true);
webview.getSettings().setUseWideViewPort(true);
webview.getSettings().setLoadWithOverviewMode(true);
webview.setWebViewClient(new MyWebViewClient());
webview.loadUrl("https://www.google.com/");
WebViewClient Code
private class MyWebViewClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("youtube") && !url.contains("-youtube")) {
Toast.makeText(context, "This is youtube url", Toast.LENGTH_SHORT).show();
return true;
} else {
String newUrl = checkUrl(url);
if (Patterns.WEB_URL.matcher(newUrl).matches()) {
Toast.makeText(context, "This is url : " + newUrl, Toast.LENGTH_SHORT).show();
view.loadUrl(newUrl);
} else {
Toast.makeText(context, "This is url : " + url, Toast.LENGTH_SHORT).show();
view.loadUrl(String.format("http://google.com/search?tbm=vid&q=%s -youtube -site:youtube.com", new Object[]{url}));
}
return false;
}
}
}
public String checkUrl(String str) {
if (str == null) {
return str;
}
StringBuilder stringBuilder;
if (Build.VERSION.SDK_INT < 28) {
if (!str.startsWith("http")) {
stringBuilder = new StringBuilder();
stringBuilder.append("http://");
stringBuilder.append(str);
str = stringBuilder.toString();
}
return str;
} else if (str.startsWith("https")) {
return str;
} else {
if (str.startsWith("http")) {
return str.replaceFirst("http", "https");
}
stringBuilder = new StringBuilder();
stringBuilder.append("https://");
stringBuilder.append(str);
return stringBuilder.toString();
}
}
NOTE - I can get link of current loaded url but not able to get link of playing video in the current url in webview.
Upvotes: 5
Views: 5318
Reputation: 54
I know it's way too late to answer this question but this is for anyone who runs into this same problem.
Firstly do not use "shouldOverrideUrlLoading(WebView view, String url)"
Instead use "shouldInterceptRequest(WebView view, WebResourceRequest request)"
so your code should be
private class MyWebViewClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
@Override
public shouldInterceptRequest(WebView view, WebResourceRequest request){
super.shouldInterceptRequest(view,request)
//Get the request and assign it to a string
String requestUrl = request.getUrl.toString;
//Get the mime-type from the string
String extension = MimeTypeMap.getFileExtensionFromUrl(requestUrl);
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
//make sure the mime-type isn't null
if(mimeType != null){
// check if any of the requestUrls contain the url of a video file
if(mimeType.startsWith("video/") && requestUrl.contains("youtube.com")){
Log.e("Video File" , requestUrl);
}
}
}
This should give the exact url of the playing video, if the video is a stream then it'll give you multiple url.
Upvotes: 4