简体   繁体   English

如何在javascript的audioSourceNode上实现播放/暂停按钮?

[英]How do I implement play/pause buttons on an audioSourceNode in javascript?

I'm having trouble implementing simple play and pause buttons on this SourceNode using JavaScript. 我无法使用JavaScript在此SourceNode上实现简单的播放和暂停按钮。

This is my audioManipulater.js file, everything gets viewed on index.php and styled on style.css. 这是我的audioManipulater.js文件,所有内容都可以在index.php上查看,并在style.css上设置样式。

So back to my js file. 回到我的js文件。 If you look at the _visualize method on the 1st line of the source. 如果您查看源代码第一行上的_visualize方法。 In the method the audioBufferSourceNode holds the file that has been loaded. 在该方法中, audioBufferSourceNode保存已加载的文件。

On line 13 the start() and stop() methods are being used on the audio node. 在第13行上,在音频节点上使用了start()stop()方法。 How can I get a reference to the audioBufferSourceNode out of the library so that I can call these methods to play and pause the sound somehow?. 如何从库中获取对audioBufferSourceNode的引用,以便可以调用这些方法以某种方式播放和暂停声音?

 _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);
 },

I'll edit-in my code for index.php & style.css upon request if needed. 如果需要,我将根据需要编辑index.php和style.css的代码。

EDIT full code: 编辑完整代码:

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

For some reason the visualizer doesn't work on jsfiddle, so here is a link to a live working demo: 由于某种原因,可视化工具无法在jsfiddle上运行,因此,这里是一个实时工作演示的链接:

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

This is a scoping issue. 这是一个范围界定问题。 From the MDN entry about variable declarations : 有关变量声明MDN条目中

The scope of a variable declared with var is its current execution context, which is either the enclosing function or, for variables declared outside any function, global. var声明的变量的范围是其当前的执行上下文,它是封闭的函数,或者对于在任何函数之外声明的变量,都是全局的。

Assigning a value to an undeclared variable implicitly creates it as a global variable (it becomes a property of the global object) when the assignment is executed. 在执行赋值时,将值分配给未声明的变量会隐式地将其创建为全局变量(它成为全局对象的属性)。

As you are declaring the variable like var audioBufferSourceNode = ... , then you are making it a local variable to the _visualize method, and that's the reason why you cannot access it from out of the library . 在声明var audioBufferSourceNode = ...之类的变量时, _visualize_visualize方法的局部变量,这就是无法从库中访问它的原因。

What you can do: 你可以做什么:

  1. Remove the var keyword for that variable declaration: 删除该变量声明的var关键字:

     audioBufferSourceNode = audioContext.createBufferSource() 
  2. Declare the variable globally (according to the explanation above, this step would be optional as the variable will implicitly be created as a global variable). 全局声明变量(根据上面的解释,此步骤将是可选的,因为该变量将隐式创建为全局变量)。

With this, you would be making audioBufferSourceNode a global variable and that implies certain risks (eg: there could be conflicts with other variables, it could be manipulated by any other methods, it is more difficult to maintain and debug if there are errors). 这样,您将把audioBufferSourceNode为全局变量,这意味着某些风险(例如:可能与其他变量发生冲突,可以通过任何其他方法进行操作,如果存在错误,则更难以维护和调试)。

You should consider @Palpatim 's suggestion below, keep the variable encapsulated in the module, and create an accessor so you can get and manipulate it from outside the module itself. 您应该在下面考虑@Palpatim的建议,将变量封装在模块中,并创建访问器,以便可以从模块本身之外获取和操作它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM