HTML5 <audio> tag not working in Android Webview

。_饼干妹妹 提交于 2019-11-30 04:04:17

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.

this seems like a very stupid bug inside the webview. One workaround currently is to "encode" the audio as an mp4 file and then link it via the video tag.

<video width="100%" height="48" controls>
     <source src="audioonly.mp4" type="video/mp4">
</video>

This will show a black rectangle (no video) and also the controls inside. It is by far not the best solution but the easiest. You can use Miro Video Converter on Mac and Windows to create such a mp4.

Good luck :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!