首页 > 代码库 > js调用播放音乐

js调用播放音乐

/*声音的js*/
$(function () {
    var file = [];
    file[‘mp3‘] = ‘../../Content/music/1.mp3‘;
    audioplayer(‘audioplane‘, file, true);
    $("#voicebtn").click(function () {
        if ($(this).hasClass("voiceBtn")) {
            $(this).removeClass("voiceBtn").addClass("noVoiceBtn");
            audioplayer(‘audioplane‘);
        } else {
            $(this).removeClass("noVoiceBtn").addClass("voiceBtn");
            audioplayer(‘audioplane‘, file, true);
        }
    });
   
   
});
/*
加注
跟注
弃牌
全压
其他声音
*/
function Voice() {
   
}
/*
    用法示例:
    var file = [];
    file[‘mp3‘] = ‘1.mp3‘;
    file[‘ogg‘] = ‘1.ogg‘;
    // 播放
    audioplayer(‘audioplane‘, file, true);
     // 停止
    audioplayer(‘audioplane‘);
*/
/** 音乐播放器 * @param obj 播放器id * @param file 音频文件 mp3: ogg: * @param loop 是否循环 */ 
function audioplayer(id, file, loop){
    var audioplayer = document.getElementById(id);
    if(audioplayer!=null)
    {
        document.body.removeChild(audioplayer);
    }
    if(typeof(file)!=‘undefined‘)
    {
        if(navigator.userAgent.indexOf("MSIE")>0)
        {
            // IE
            var player = document.createElement(‘bgsound‘);
            player.id = id;
            player.src = http://www.mamicode.com/file[‘mp3‘];
            player.setAttribute(‘autostart‘, ‘true‘);
            if(loop){ player.setAttribute(‘loop‘, ‘infinite‘);
            }
            document.body.appendChild(player);
        }else{
            // Other FF Chome Safari Opera
            var player = document.createElement(‘audio‘);
            player.id = id;
            player.setAttribute(‘autoplay‘, ‘autoplay‘);
            if (loop) {
                player.setAttribute(‘loop‘, ‘loop‘);
            }
            document.body.appendChild(player);
            var mp3 = document.createElement(‘source‘);
            mp3.src = http://www.mamicode.com/file[‘mp3‘];
            mp3.type = ‘audio/mpeg‘;
            player.appendChild(mp3);
            var ogg = document.createElement(‘source‘);
            ogg.src = http://www.mamicode.com/file[‘ogg‘];
            ogg.type = ‘audio/ogg‘;
            player.appendChild(ogg);
        }
    }
}

 

js调用播放音乐