Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 <audio> tag not working in Android Webview

the HTML5 audio tag doesn't seem to work in the web view if the source file is stored locally. i.e.

<audio controls="controls">
   <source src="my_local_audio_file.mp3" type="audio/mpeg" />
</audio>

It works when the file is accessed from a server like src="http://example.com/audio.mp3"

I am trying it on Android ICS.

There are a few workarounds as stated here . But i don't understand why this problem is occurring.

Any solutions or insights as to why this is happening?

Thank you.

like image 768
varun1505 Avatar asked Mar 19 '13 08:03

varun1505


1 Answers

this is just alternet way to achieve goal to play audio from html file

use Mediaplayer (http://developer.android.com/reference/android/media/MediaPlayer.html) of Android for playing audio. you can invoke function of android from javascript that you have written in HTML file..

this is example of how you can invoke function written in java file from javascript code

WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
--------------------------------
public class WebAppInterface {

    Context mContext;
    /** Instantiate the interface and set the context */
    WebAppInterface(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page */
    @JavascriptInterface
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }
}

---------------------------
java sript code

<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

<script type="text/javascript">
    function showAndroidToast(toast) {
        Android.showToast(toast);
    }
</script>

This way you invoke audio from Android code.

like image 100
Viral Parekh Avatar answered Oct 20 '22 12:10

Viral Parekh