【发布时间】:2021-07-21 00:34:07
【问题描述】:
我一直在寻找如何在 webview 中播放来自任何网站的音频?
我注意到 Chrome 浏览器正在播放音频,但我的 WebView 应用没有播放。 你能提供我任何资源或代码sn-p吗?
提前致谢!
【问题讨论】:
标签: android webview audio-player
我一直在寻找如何在 webview 中播放来自任何网站的音频?
我注意到 Chrome 浏览器正在播放音频,但我的 WebView 应用没有播放。 你能提供我任何资源或代码sn-p吗?
提前致谢!
【问题讨论】:
标签: android webview audio-player
试试这个,希望它能解决你的问题。
WebView webView = findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient());
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowContentAccess(true);
webSettings.setDomStorageEnabled(true);
webView.loadUrl(url);
另外,将android:hardwareAccelerated="true" 添加到清单中的activity tag 中。
【讨论】:
这可以通过Javascript接口完成
创建类 WebInterface
public class WebInterface{
Context mContext;
WebInterface(Context c) {
mContext = c;
}
@JavascriptInterface
public void playSound(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
@JavascriptInterface
public void pauseSound(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
In your WebView class
WebView browser;
browser=(WebView)findViewById(R.id.webkit);
browser.getSettings().setJavaScriptEnabled(true);
browser.addJavascriptInterface(new WebInterface(this), "Android");
browser.loadUrl("http://someurl.com");
In HTML code
<html>
<head>
<script type="text/javascript">
function playSound(toast) {
Android.showToast(toast);
}
function pauseSound(toast) {
Android.showToast(toast);
}
</script>
</head>
<body>
<input type="button" value="Say hello" onClick="playSound('Sound Played!')" />
<input type="button" value="Say hello" onClick="pauseSound('Sound Paused!')" />
</body>
</html>
【讨论】: