bgm.start(); var bgm = { typedef : ['ji','niu','yang','niao'], buffer : {}, context : null, timeout : 3000, start: function(){ this.cacheBuf(); this.run(); }, run: function(){ var _this = this; setTimeout(function(){ if(_this.context != null){ _this.context.close(); _this.context = null; } var _li = Math.floor((Math.random()*_this.typedef.length)); _this.audioPlay(_this.typedef[_li]); _this.run(); }, _this.timeout); }, audioPlay: function(tp){ var _this = this; var _buff = _this.buffer[tp].slice(0, _this.buffer[tp].byteLength); _this.context = new (window.AudioContext || window.webkitAudioContext)(); _this.context.decodeAudioData(_buff, function(buffer){ var source = _this.context.createBufferSource(); source.buffer = buffer; source.connect(_this.context.destination); source.start(0); }); }, cacheBuf: function(){ var _this = this, _tp; for(i in _this.typedef){ _tp = _this.typedef[i]; _this.getbuf('static/audio/'+ _tp +'.mp3', _tp); } }, getbuf: function(url, tp){ var _this = this; var xhr = new XMLHttpRequest(); xhr.open('GET', url, true); xhr.responseType = 'arraybuffer'; xhr.onreadystatechange = function () { if (xhr.readyState === 4 && xhr.status === 200) { _this.buffer[tp] = xhr.response; } }; xhr.send(); } };
网页缓存音频文件循环播放,背景音乐循环播放
猜你喜欢
转载自www.cnblogs.com/6min/p/12411398.html
今日推荐
周排行