无法在 Android WebView 中播放来自 URL 的 mp3
Can't play mp3 from URL in Android WebView
我正在尝试在 WebView 中播放 mp3 文件。但是,该文件在 WebView 中不播放,并且在输出和错误后页面挂起(请参阅下面的日志输出)。
jQuery代码
$.playSound('../static/sounds/cash-register.mp3');
$('#myAudio').on('ended', function() {
window.location.replace("scan_tablet");
});
(function($){
$.extend({
playSound: function(){
return $("<embed src='"+arguments[0]+"' hidden='true' autostart='true' loop='false' class='playSound'>" + "<audio id='myAudio' autoplay='autoplay' style='display:none;' controls='controls'><source src='"+arguments[0]+"' type=\"audio/mpeg\" /><source src='"+arguments[0]+".ogg' type=\"audio/ogg\" /></audio>").appendTo('body');
}
});
})(jQuery);
WebView 客户端
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAllowFileAccessFromFileURLs(true);
if (Build.VERSION.SDK_INT >= 19) {
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
}
webView.setWebViewClient(new WebViewClient());
等...
日志输出
1) D/WebViewCallback: shouldInterceptRequest=localhost/static/sounds/cash-register.mp3
2) D/WebViewCallback: onLoadResource=localhost/static/sounds/cash-register.mp3
3) [WARNING:aw_network_delegate.cc(66)] localhost/static/sounds/cash-register.mp3#-3#1
4) E/MediaResourceGetter: Invalid url: java.lang.NumberFormatException: Invalid int: "null"
我不确定为什么 URL 似乎在日志输出的第 3 行中被修改。
当我在笔记本电脑上使用 Chrome 尝试此操作时,mp3 文件播放没有任何问题。
在进一步深入了解 WebView 文档后,我尝试在 WebView 客户端上进行以下设置。
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
此设置默认设置为 TRUE。我认为这是在阻止播放声音。
我正在尝试在 WebView 中播放 mp3 文件。但是,该文件在 WebView 中不播放,并且在输出和错误后页面挂起(请参阅下面的日志输出)。
jQuery代码
$.playSound('../static/sounds/cash-register.mp3');
$('#myAudio').on('ended', function() {
window.location.replace("scan_tablet");
});
(function($){
$.extend({
playSound: function(){
return $("<embed src='"+arguments[0]+"' hidden='true' autostart='true' loop='false' class='playSound'>" + "<audio id='myAudio' autoplay='autoplay' style='display:none;' controls='controls'><source src='"+arguments[0]+"' type=\"audio/mpeg\" /><source src='"+arguments[0]+".ogg' type=\"audio/ogg\" /></audio>").appendTo('body');
}
});
})(jQuery);
WebView 客户端
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAllowFileAccessFromFileURLs(true);
if (Build.VERSION.SDK_INT >= 19) {
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
}
webView.setWebViewClient(new WebViewClient());
等...
日志输出
1) D/WebViewCallback: shouldInterceptRequest=localhost/static/sounds/cash-register.mp3
2) D/WebViewCallback: onLoadResource=localhost/static/sounds/cash-register.mp3
3) [WARNING:aw_network_delegate.cc(66)] localhost/static/sounds/cash-register.mp3#-3#1
4) E/MediaResourceGetter: Invalid url: java.lang.NumberFormatException: Invalid int: "null"
我不确定为什么 URL 似乎在日志输出的第 3 行中被修改。
当我在笔记本电脑上使用 Chrome 尝试此操作时,mp3 文件播放没有任何问题。
在进一步深入了解 WebView 文档后,我尝试在 WebView 客户端上进行以下设置。
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
此设置默认设置为 TRUE。我认为这是在阻止播放声音。