我有一个HTML音频元素,我动态地将元素的“src”属性设置为存储在我们局域网中的音频文件。
这是它的工作原理:
// Recieve extra from SecondActivity.class
Intent thirdActivityintent = getIntent();
String mString = thirdActivityintent.getExtra().getString("sentString");
// This time I am calling SecondActivity.class but I will not send extra
thirdActivityintent = new Intent(ThirdActivity.this, SecondActivity.class);
startActivity(thirdActivityintent);
有时,我指向的源音频文件链接断开,这会导致生成404错误并将其记录到浏览器控制台。
我希望能够捕获404错误,以防止它们被记录到控制台。
这是我尝试的方式:
function setSource(source) {
audio.src = source;
}
var audio = new Audio();
var source = "http://localhost/folder/file.mp3";
setSource(source);
不幸的是,我的try / catch语句什么都不做,错误仍然记录在控制台上。我做错了吗?
由于我的应用程序的性质,会有很多404错误,这是正常的和预期的,但它看起来非常不稳定并且对用户“丑陋”(如果他们碰巧打开控制台)。
仅供参考:我使用的是谷歌浏览器。
答案 0 :(得分:1)
audio.onload = function() {
console.log('success');
};
audio.onerror = function() {
console.log('fail');
};
audio.src = 'http://localhost/folder/file.mp3';
答案 1 :(得分:1)
在这种情况下,浏览器控制台中的HTTP错误记录是浏览器独有的功能,而不是Javascript或任何其他网站代码。
这无法避免。
答案 2 :(得分:0)
是的。通过最近的更新,这是可能的。您可以在此处启用它:DevTools->Settings->General->Console->Hide network messages。