5

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.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Jitesh Prajapati
  • 2,533
  • 4
  • 29
  • 51
  • shouldOverrideUrlLoading method calls when a url is goint to be loaded and it will not show every url from your loaded HTML content. To get and check the url inside the webview content you can try https://stackoverflow.com/questions/8200945/how-to-get-html-content-from-a-webview. once you are getting the conent then you can add a string operation to fetch a specific pattern of url such as video and keep in a list (if want all). – Hari N Jha Jun 07 '19 at 07:55
  • 1
    Did you ever find a solution to this? I am running into the same issue – PGMacDesign Feb 06 '20 at 21:13

1 Answers1

4

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.

Daniel
  • 54
  • 1
  • 3
  • 1
    The few YouTube videos I've worked with produce a null extension, as well as mimetype. Don't think this is a foolproof method. – saltandpepper Jan 31 '22 at 17:01