Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Thẻ HTML5 <audio> không hoạt động trong Android WebView


Dùng MediaPlayer của Android để phát audio. Bạn cần gọi hàm Android từ JavaScript đã viết trong file HTML.

WebView wv = (WebView) findViewById(R.id.webview); wv.addJavascriptInterface(new WebAppInterface(this), "Android"); public class WebAppInterface {    Context mContext;    WebAppInterface(Context c) {       mContext = c;    }    @JavascriptInterface    public void showToast(String toast) {       Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();    } }

JavaScript của tôi:

<input type = "button" value = "Demo" onClick = "showAndroidToast('Hello Android!')" /> <script>    function showAndroidToast(toast) {       Android.showToast(toast);    } </script>