Android在Api Level 17增加了一个WebView的配置:
WebSettings.getMediaPlaybackRequiresUserGesture();
WebSettings.setMediaPlaybackRequiresUserGesture(boolean);
详见android dev文档:
https://developer.android.com/reference/android/webkit/WebSettings.html#getMediaPlaybackRequiresUserGesture() https://developer.android.com/reference/android/webkit/WebSettings.html#setMediaPlaybackRequiresUserGesture(boolean)
简单地说,在Api Level 17以后,android增加了对于由WebView加载的网页播放声音是否需要用户的手势触发。并且,关键是,默认值为true,也就是,如果你不做手动设置false,那么将需要手势才能播放触发音效,也就是说,不能自动播放。
代码类似:
WebView webview = ... // your webview
WebSettings settings = webview.getSettings();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
settings.setMediaPlaybackRequiresUserGesture(false);
}