HTML5 Audio Tag Not Working in Android WebView



Use Mediaplayer of Android for playing audio. You need to call function of Android from JavaScript that you have written in HTML file.

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

The following is my JavaScript:

<input type = "button" value = "Demo" onClick = "showAndroidToast('Hello Android!')" />
<script>
   function showAndroidToast(toast) {
      Android.showToast(toast);
   }
</script>
Updated on: 2020-01-28T08:31:19+05:30

575 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements