Html 在使用mp3之外的firefox时使用其他音效源
我正在尝试在用户点击后制作一个有声音的菜单。它在Chrome、safari甚至IE中都能很好地工作,但在Mozilla Firefox中却不行。我使用mp3文件播放firefox无法播放的声音。这是密码Html 在使用mp3之外的firefox时使用其他音效源,html,firefox,audio,mp3,Html,Firefox,Audio,Mp3,我正在尝试在用户点击后制作一个有声音的菜单。它在Chrome、safari甚至IE中都能很好地工作,但在Mozilla Firefox中却不行。我使用mp3文件播放firefox无法播放的声音。这是密码 function loadSound (src) { var sound = document.createElement("audio"); if ("src" in sound) { sound.autoPlay = false; }
function loadSound (src) {
var sound = document.createElement("audio");
if ("src" in sound) {
sound.autoPlay = false;
}
else {
sound = document.createElement("bgsound");
sound.volume = -10000;
sound.play = function () {
this.src = src;
this.volume = 0;
}
}
sound.src = src;
document.body.appendChild(sound);
return sound;
}
$(document).ready(function () {
var sound = loadSound("<?=base_url()?>sound/testing.mp3"); // preload
$(".main_nav a").click(function(){
var ids = $(this).attr("id").split("-");
var url = ids[2];
sound.play();
setTimeout (function(){window.location = url;}, 2000);
return false;
});
});
函数loadSound(src){
var sound=document.createElement(“音频”);
如果(“src”在声音中){
sound.autoPlay=false;
}
否则{
声音=document.createElement(“bgsound”);
音量=-10000;
sound.play=函数(){
this.src=src;
这个体积=0;
}
}
sound.src=src;
文件.正文.附件(声音);
回音;
}
$(文档).ready(函数(){
var sound=loadSound(“sound/testing.mp3”);//预加载
$(“.main_nav a”)。单击(函数(){
var id=$(this.attr(“id”).split(“-”);
var url=ids[2];
声音。播放();
setTimeout(函数(){window.location=url;},2000);
返回false;
});
});
什么类型的音频文件在firefox中工作?当用户使用firefox时,如何更改源文件?好的,我理解您的问题,我有一个解决方案。 你可以使用我做的这个函数:
function getSupportedAudio(){
var testEl = document.createElement( "audio" ), mp3, ogg;
if ( testEl.canPlayType ) {
mp3 = "" !== testEl.canPlayType( 'audio/mpeg;' );
ogg = "" !== testEl.canPlayType( 'audio/ogg' );
}
if(mp3){
return ".mp3";
}
else{
return ".ogg";
}
}
在函数loadSound(src)中,使用以下命令:
sound.src = src+getSupportedAudio();
document.body.appendChild(sound);
当您调用loadSound函数时,不要在参数中发送音频格式(但您需要将ogg和mp3这两个文件放在同一文件夹中并使用相同的名称):
var-sound=loadSound(“声音/测试”);//预载
除了像您的答案一样使用外,还有其他方法吗?是否可以将sound.src作为数组?
var sound = loadSound("<?=base_url()?>sound/testing"); // preload