• 网页缓存音频文件循环播放,背景音乐循环播放


    bgm.start();
    
    var bgm = {
        typedef : ['ji','niu','yang','niao'],
        buffer  : {},
        context : null,
        timeout : 3000,
        start: function(){
            this.cacheBuf();
            this.run();
        },
        run: function(){
            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(){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();
        }
    };
  • 相关阅读:
    上传文件,经过Zuul,中文文件名乱码解决办法
    Spring Cloud Sleuth进阶实战
    如何在IDEA启动多个Spring Boot工程实例
    深入理解Zuul之源码解析
    深入理解Hystrix之文档翻译
    Spring 之BeanFactory(转)
    spring之BeanFactoryAware接口
    ExecutorService线程池
    POJO
    newInstance 与new的区别
  • 原文地址:https://www.cnblogs.com/6min/p/12411398.html
Copyright © 2020-2023  润新知