开发者

Playing an MP4 or MP3 from a Link within an Android WebView

I have searched far and wide and can't seem to find the exact answer or even close.

I have a WebView that opens a webpage (JQuery Mobile) and all is well.

Within that page there are static links to both mp3 and mp4 files. The idea is that when clicked - the device will open the default sound or movie app to play each. This works when using the Android browser without issue.

When I open inside WebView, and click the same links - nothing happens. Nothing opens, or plays.

I'm setting getSettings as wide open as I can.

**web.getSettings().setJavaScriptEnabled(true); 
    web.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    web.getSettings().setPluginsEnabled(true);
    web.getSettings().setSupportMultip开发者_运维技巧leWindows(true);
    web.getSettings().setSupportZoom(true);
    web.getSettings().setBuiltInZoomControls(true);
    web.getSettings().setAllowFileAccess(true);
    web.setVerticalScrollBarEnabled(false);
    web.setHorizontalScrollBarEnabled(false);**

I'll also note that I believe "shouldOverrideUrlLoading" only works on the initial URL loaded in the WebView. This is a link inside that WebView - so does not seem to work.

I had tried that.

Any ideas on how to enable the user the ability to click a link within a WebView and open the associate application as the browser does?

Much Appreciated!

/r


Ok - found the answer after much searching on different topics.

The answer lies in intercepting the link as such:

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


you will use an interface that loads a new Activity with a VideoView that executes the media link and blah blah blah....but i think this is the easy way to do it...

WebView WebContent = (WebView)findViewById(R.id.myWebView);

String htmlLink= "<a href=\"http://prueba.agenciareforma.com/webs/elnorte/blackberry/mp3/uno.mp3\">Open Mp3 here!</a>";

        WebContent.loadDataWithBaseURL(null,  htmlLink, "text/html", "utf-8", null);                
        WebSettings webSettings = WebContent.getSettings();
        webSettings.setSavePassword(false);
        webSettings.setSaveFormData(false);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(true);     

UPDATE:: change::

<a href="planetofrock.com/samples/lballad.mp3">Click Here</a>

to::

<a href="http://planetofrock.com/samples/lballad.mp3">Click Here</a>


I have a similar page which is loading a URL and that URL contains a link to play the audio in it.Its working fine for the first time but second time it doesn't play. the code for webview is:

WebSettings webSettings = this.webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
this.webview.setWebViewClient(new WebViewClient());
this.webview.setWebChromeClient(new WebChromeClient());
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);

on my page I have audio tag like

<audio id="audiotag1"  style="display:none" preload="auto" hidden='true' autostart='false'>
    <source src="../../Content/sound/NewOrder.wav" type="audio/x-wav"> 
</audio>

and I am playing it through JQuery by calling its method

$('#audiotag1').play();

Hope it will work for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜