Как полностью уничтожить объекты AudioContext и BufferNode, включая его буфер - PullRequest
0 голосов
/ 05 января 2019

Может ли этот код быть правдой? Есть хорошие идеи? this.http - это объект lib ajax, так что позаботьтесь об этом. 'res' - это аудиоданные, запрашиваемые с сервера.

window['AudioContext'] = window['AudioContext'] || window['webkitAudioContext']
        this.http.get(audio.sourceUrl, {
            responseType: 'arraybuffer'
        }).subscribe((res) => {
            if (this.source) {
                this.source.disconnect(0)
                this.source.buffer = null
            }
            if (this.audioCtx) {
                this.audioCtx.close()
            }

            this.audioCtx = new AudioContext()

            this.audioCtx.decodeAudioData(res, (buffer) => {
                this.source = this.audioCtx.createBufferSource() // creates a sound source
                this.source.buffer = buffer                    // tell the source which sound to play
                this.source.connect(this.audioCtx.destination)       // connect the source to the context's destination (the speakers)
                this.source.start(0)
            }, this.onError)
        })

Я видел чей-то код, подобный этому, но я не знаю, почему он попробовал его таким образом:

var ctx = new AudioContext(),
    scratchBuffer = ctx.createBuffer(1, 1, 22050);

    class WebAudioAdapter extends AudioAdapter {
        close() {
            if( this.__src ) {
                this.__src.onended = null;
                this.__src.disconnect(0);
                try { this.__src.buffer = scratchBuffer; } catch(e) {}
                this.__src = null;
            }
        }
    }
...