Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView won't play any sound

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.

like image 914
John Doe Avatar asked Jan 31 '18 13:01

John Doe


1 Answers

Try to add this

webSettings.setMediaPlaybackRequiresUserGesture(false)
like image 159
Pavel Poley Avatar answered Oct 08 '22 02:10

Pavel Poley