Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 歌剧中的声音提示_Javascript_Jquery_Html_Html5 Audio - Fatal编程技术网

Javascript 歌剧中的声音提示

Javascript 歌剧中的声音提示,javascript,jquery,html,html5-audio,Javascript,Jquery,Html,Html5 Audio,我正在尝试在Opera 12.16(目前最新版本)中播放音频元素,但它与ogg元素配合使用 这是我目前只处理mp3文件而不处理Opera的小提琴: 解决方案似乎是这样做的(如中所述): 为了添加ogg文件,我尝试生成所需的结构,但没有成功: var audioElement = document.createElement('audio'); var audioSource1 = document.createElement('source'); var audioSource2 = docu

我正在尝试在Opera 12.16(目前最新版本)中播放
音频
元素,但它与
ogg
元素配合使用

这是我目前只处理mp3文件而不处理Opera的小提琴:

解决方案似乎是这样做的(如中所述):

为了添加
ogg
文件,我尝试生成所需的结构,但没有成功:

var audioElement = document.createElement('audio');
var audioSource1 = document.createElement('source');
var audioSource2 = document.createElement('source');

//mp3 file
audioSource1.setAttribute('src', "http://"+ document.domain +"/files/notification2.mp3");
audioSource1.setAttribute('type', 'audio/mpeg');

//ogg file
audioSource2.setAttribute('src', "http://"+ document.domain +"/files/notification2.ogg");
audioSource2.setAttribute('type', 'audio/ogg');

audioElement.setAttribute('autoplay', 'autoplay');

audioElement.append(audioSource2); //opera
audioElement.append(audioSource1);

//audioElement.load()
$.get();
audioElement.addEventListener("load", function() {
    audioElement.get(0).play();
}, true);

如何使它在Opera中与
ogg
文件一起工作?

我会检查用户使用的浏览器,然后选择源

var audioElement = document.createElement('audio');
if (navigator.userAgent.search("MSIE") >= 0) {
        audioSource1.setAttribute('src', "http://" + document.domain + "/files/notification2.mp3");
        audioSource1.setAttribute('type', 'audio/mpeg');
 } else if (navigator.userAgent.search("Chrome") >= 0) {
     audioSource2.setAttribute('src', "http://" + document.domain + "/files/notification2.ogg");
     audioSource2.setAttribute('type', 'audio/ogg');
 } else if (navigator.userAgent.search("Firefox") >= 0) {
     audioSource2.setAttribute('src', "http://" + document.domain + "/files/notification2.ogg");
     audioSource2.setAttribute('type', 'audio/ogg');
 } else if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) {
     audioSource1.setAttribute('src', "http://" + document.domain + "/files/notification2.mp3");
     audioSource1.setAttribute('type', 'audio/mpeg');
 } else if (navigator.userAgent.search("Opera") >= 0) {
     audioSource2.setAttribute('src', "http://" + document.domain + "/files/notification2.ogg");
     audioSource2.setAttribute('type', 'audio/ogg');
 }
 audioElement.setAttribute('autoplay', 'autoplay');
 audioElement.addEventListener("load", function () {
     audioElement.play();
 }, true);
var audioElement=document.createElement('audio');
if(navigator.userAgent.search(“MSIE”)>=0){
audioSource1.setAttribute('src',“http://“+document.domain+”/files/notification2.mp3”);
audioSource1.setAttribute('type','audio/mpeg');
}else if(navigator.userAgent.search(“Chrome”)>=0){
audioSource2.setAttribute('src',“http://“+document.domain+”/files/notification2.ogg”);
audioSource2.setAttribute('type','audio/ogg');
}else if(navigator.userAgent.search(“Firefox”)>=0){
audioSource2.setAttribute('src',“http://“+document.domain+”/files/notification2.ogg”);
audioSource2.setAttribute('type','audio/ogg');
}else if(navigator.userAgent.search(“Safari”)>=0&&navigator.userAgent.search(“Chrome”)<0){
audioSource1.setAttribute('src',“http://“+document.domain+”/files/notification2.mp3”);
audioSource1.setAttribute('type','audio/mpeg');
}else if(navigator.userAgent.search(“Opera”)>=0){
audioSource2.setAttribute('src',“http://“+document.domain+”/files/notification2.ogg”);
audioSource2.setAttribute('type','audio/ogg');
}
setAttribute('autoplay','autoplay');
audioElement.addEventListener(“加载”,函数(){
audioElement.play();
},对);

我希望我正确理解你的问题

使用功能检测而不是浏览器检测:

function isMpeg()
{
    var a = document.createElement('audio');
    return !!(a.canPlayType && a.canPlayType('audio/mpeg;').replace(/no/, ''));
}


function isOgg()
{
    var a = document.createElement('audio');
    return !!(a.canPlayType && a.canPlayType('audio/ogg; codecs="vorbis"').replace(/no/, ''));
}

function isAAC()
{
    var a = document.createElement('audio');
return !!(a.canPlayType && a.canPlayType('audio/mp4; codecs="mp4a.40.2"').replace(/no/, ''));
}

alert(isMpeg());
alert(isOgg());
alert(isAAC());
例如:

另见:

或使用

var audioElement = document.createElement('audio');
if (navigator.userAgent.search("MSIE") >= 0) {
        audioSource1.setAttribute('src', "http://" + document.domain + "/files/notification2.mp3");
        audioSource1.setAttribute('type', 'audio/mpeg');
 } else if (navigator.userAgent.search("Chrome") >= 0) {
     audioSource2.setAttribute('src', "http://" + document.domain + "/files/notification2.ogg");
     audioSource2.setAttribute('type', 'audio/ogg');
 } else if (navigator.userAgent.search("Firefox") >= 0) {
     audioSource2.setAttribute('src', "http://" + document.domain + "/files/notification2.ogg");
     audioSource2.setAttribute('type', 'audio/ogg');
 } else if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) {
     audioSource1.setAttribute('src', "http://" + document.domain + "/files/notification2.mp3");
     audioSource1.setAttribute('type', 'audio/mpeg');
 } else if (navigator.userAgent.search("Opera") >= 0) {
     audioSource2.setAttribute('src', "http://" + document.domain + "/files/notification2.ogg");
     audioSource2.setAttribute('type', 'audio/ogg');
 }
 audioElement.setAttribute('autoplay', 'autoplay');
 audioElement.addEventListener("load", function () {
     audioElement.play();
 }, true);
function isMpeg()
{
    var a = document.createElement('audio');
    return !!(a.canPlayType && a.canPlayType('audio/mpeg;').replace(/no/, ''));
}


function isOgg()
{
    var a = document.createElement('audio');
    return !!(a.canPlayType && a.canPlayType('audio/ogg; codecs="vorbis"').replace(/no/, ''));
}

function isAAC()
{
    var a = document.createElement('audio');
return !!(a.canPlayType && a.canPlayType('audio/mp4; codecs="mp4a.40.2"').replace(/no/, ''));
}

alert(isMpeg());
alert(isOgg());
alert(isAAC());