HTML5
使用 Android 的 Mediaplayer 來播放音訊。您需要從 HTML 檔案中編寫的 JavaScript 呼叫 Android 的函式。
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
<input type = "button" value = "Demo" onClick = "showAndroidToast('Hello Android!')" /> <script> function showAndroidToast(toast) { Android.showToast(toast); } </script>
廣告