我如何调用start()&这里有stop()方法来创建功能音频控件吗?

时间:2015-06-25 12:22:52

标签: javascript html audio web-audio

我有一个方法audioBufferSourceNode,它保存已加载的音频文件。

136 which is line 13 below行上,start()stop()方法正在音频节点上用于其他事情,但我无法做到另外做。如何调用这些方法播放和暂停音频。我不知道拨打start()&的正确方法。 stop()方法,以便我有播放/暂停音频的按钮或div,以及如何使用这些方法来设置音量滑块和静音按钮。我该怎么做呢?

旁注:我被告知声明变量' audioBufferSourceNode'全球范围内更好的做法,但不确定如何或它们的确切含义,或者甚至与我的问题有什么关系。

因此在line 13上,start()stop()方法正在音频节点上使用。

 _visualize: function(audioContext, buffer) {
    var audioBufferSourceNode = audioContext.createBufferSource(),
        analyser = audioContext.createAnalyser(),
        that = this;
    //connect the source to the analyser
    audioBufferSourceNode.connect(analyser);
    //connect the analyser to the destination(the speaker), or we won't hear the sound
    analyser.connect(audioContext.destination);
    //then assign the buffer to the buffer source node
    audioBufferSourceNode.buffer = buffer;
    //play the source
    if (!audioBufferSourceNode.start) {
        audioBufferSourceNode.start = audioBufferSourceNode.noteOn //in old browsers use noteOn method
        audioBufferSourceNode.stop = audioBufferSourceNode.noteOff //in old browsers use noteOn method
    };
    //stop the previous sound if any
    if (this.animationId !== null) {
        cancelAnimationFrame(this.animationId);
    }
    if (this.source !== null) {
        this.source.stop(0);
    }
    audioBufferSourceNode.start(0);
    this.status = 1;
    this.source = audioBufferSourceNode;
    audioBufferSourceNode.onended = function() {
        that._audioEnd(that);
    };
    this._updateInfo('Playing ' + this.fileName, false);
    this.info = 'Playing ' + this.fileName;
    document.getElementById('fileWrapper').style.opacity = 0.2;
    this._drawSpectrum(analyser);
 },

完整代码:

https://jsfiddle.net/4hty6kak/1/

Web Audio API并不适用于jsfiddle,所以这是一个实时工作演示:

http://wayou.github.io/HTML5_Audio_Visualizer/

您有任何简单的固体解决方案吗?

0 个答案:

没有答案