从WebView检测并播放视频URL

时间:2019-06-07 07:21:59

标签: android android-webview

我在应用程序中使用WebView来获取加载网址。我想要的功能是检测视频并在Web视图中播放任何视频时获取网址。

我已经使用过WebViewClientshouldOverrideUrlLoading(WebView view, String url)

Webview代码

    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代码

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

注意-我可以获取当前加载的URL的链接,但无法获取Webview中当前URL中的播放视频的链接。

1 个答案:

答案 0 :(得分:0)

我知道回答这个问题为时已晚,但这是针对遇到相同问题的任何人的。

首先不要使用“ shouldOverrideUrlLoading(WebView视图,字符串url)”
而是使用“ shouldInterceptRequest(WebView视图,WebResourceRequest请求)”

因此您的代码应该是

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

     }
   }
}

这应该提供正在播放的视频的确切网址,如果视频是流媒体,那么它将为您提供多个网址。