Search code examples
streamingmp3webview

open mp3 or apk file from webview


I would like to know how I can open a Mp3 file from within a webview, basically a link that points to an MP3 file which would then open up the standard media player. Is this possible? I know it is because it works on the default webbrowser so I was wondering why I can't get it to work on a standard webview. Any help would be much appreciated.


Solution

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