从Android WebView中的链接播放MP4或MP3

Rob*_*bbe 3 jquery mp3 android hyperlink webview

我搜索得很远,似乎无法找到确切的答案甚至关闭.

我有一个WebView打开一个网页(JQuery Mobile),一切都很好.

在该页面内有mp3和mp4文件的静态链接.这个想法是,当点击时 - 设备将打开默认声音或电影应用程序来播放每个.这在使用Android浏览器时没有问题.

当我在WebView中打开,并单击相同的链接 - 没有任何反应.什么都没有打开或播放.

我正在设置getSettings尽可能广泛.

**web.getSettings().setJavaScriptEnabled(true); 
    web.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    web.getSettings().setPluginsEnabled(true);
    web.getSettings().setSupportMultipleWindows(true);
    web.getSettings().setSupportZoom(true);
    web.getSettings().setBuiltInZoomControls(true);
    web.getSettings().setAllowFileAccess(true);
    web.setVerticalScrollBarEnabled(false);
    web.setHorizontalScrollBarEnabled(false);**
Run Code Online (Sandbox Code Playgroud)

我还要注意,我认为"shouldOverrideUrlLoading"仅适用于WebView中加载的初始URL.这是WebView中的一个链接 - 所以似乎不起作用.

我试过了.

有关如何使用户能够单击WebView中的链接并像浏览器一样打开关联应用程序的任何想法?

非常感激!

/ R

Rob*_*bbe 8

好的 - 经过多次搜索不同主题后找到答案.

答案在于截取链接:

     public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.endsWith(".mp3")) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse(url), "audio/*");
            view.getContext().startActivity(intent);   
            return true;
        } else if (url.endsWith(".mp4") || url.endsWith(".3gp")) {
                Intent intent = new Intent(Intent.ACTION_VIEW); 
                intent.setDataAndType(Uri.parse(url), "video/*");
                view.getContext().startActivity(intent);   
                return true;
        } else {
            return super.shouldOverrideUrlLoading(view, url);
        }
     }
Run Code Online (Sandbox Code Playgroud)