I use the simplest instance of WebView and so far everything required works well, except for playing sounds, both javascript invoked sounds and html5 audio tag do not work through this WebView.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("file:///android_asset/game.html");
}
}
I can exclude issues with the HTML side, since the sound works fine in regular browsers on both PC and phone.
Tested so far on Android 4.4 and Andoroid 7.0 both have sounds within regular browser but no sound within WebView.
Most common answer is that phones require sound to be initiated with a tap on the screen. That does not explain why regular browsers work fine though, while only WebView seems affected.
Try to add this
webSettings.setMediaPlaybackRequiresUserGesture(false)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With