首页 > 解决方案 > Android App 正在通过 Webview URL 加载视图,但未加载 URL 在后台的音频

问题描述

我有一个 Android 应用程序通过 Webview Url 加载视图。Url 中有一个背景音频。Url 正在正确加载,但未播放音频。

这是我在 html 中播放音频的 HTML 代码。

@if($booking->order_status == "Pending")
    <audio autoplay>
         <source src="notification/tune.mp3">
    </audio>
@endif

以及运行此音频的 javascript 部分。

<script>
window.onload = function() {
    var context = new AudioContext();
}
</script>

任何人都可以帮助我在我的 android 应用程序代码中添加什么,以便在使用 Webview 加载 URL 时在后台运行音频。

谢谢你。

标签: phpandroidhtmllaravelandroid-studio

解决方案


将此设置添加到 webview 对象

WebView myWebView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
webSettings.setMediaPlaybackRequiresUserGesture(false)

推荐阅读