WebView不会播放任何声音 [英] WebView won't play any sound
问题描述
我使用的是WebView的最简单实例,到目前为止,所需的所有条件都可以正常运行,除了播放声音外,JavaScript调用的声音和html5音频标签都无法在此WebView中正常工作.
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");
}
}
我可以排除HTML方面的问题,因为声音可以在PC和手机上的常规浏览器中正常运行.
I can exclude issues with the HTML side, since the sound works fine in regular browsers on both PC and phone.
到目前为止,在Android 4.4和Andoroid 7.0上进行的测试均在常规浏览器中有声音,但在WebView中无声音.
Tested so far on Android 4.4 and Andoroid 7.0 both have sounds within regular browser but no sound within WebView.
最常见的答案是电话需要通过点击屏幕来启动声音.但这不能解释为什么常规浏览器可以正常运行,而似乎只有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.
推荐答案
尝试添加此内容
webSettings.setMediaPlaybackRequiresUserGesture(false)
这篇关于WebView不会播放任何声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!