为什么我的start()和stop()方法不起作用?

时间:2015-07-04 19:11:07

标签: javascript jquery html5 audio

为什么我的start()和stop()方法不起作用?我无法暂停播放音频,有人可以看到我做错了吗?

在js中,$替换为jQuery

继续前进这是我的代码,我已经使用过:

            $('.play').click(function() {
                audioBufferSouceNode.start();   
            });

            $('.pause').click(function() {
                audioBufferSouceNode.stop();
            });

            $('.volumeMax').click(function() {
                audioBufferSouceNode.volume=1;
            });

            $('.volumestop').click(function() {
                audioBufferSouceNode.volume=0;
            });

            $('.playatTime').click(function() {
                audioBufferSouceNode.currentTime= 35;
                audioBufferSouceNode.play();
            });     

但由于某种原因,它无法正常工作。这是我的index.php页面。

           <div id="wrapper">
        <div id="fileWrapper" class="file_wrapper">
            <div id="info">
                HTML5 Audio API showcase | An Audio Viusalizer
            </div>
            <label for="uploadedFile">Drag&drop or select a file to play:</label>
            <input type="file" id="uploadedFile"></input>
        </div>
        <div id="visualizer_wrapper">
            <canvas id='canvas' width="800" height="350"></canvas>
        </div>
    </div>
    <footer>
    </footer>
   <div class="audioPlayer">  

        <button type="button" class="play">play</button>
        <button type="button" class="pause">pause</button>
        <button type="button" class="playatTime">play at 35 seconds</button>
        <button type="button" class="volumestop">Volume to 0</button>
        <button type="button" class="volumeMax">Volume open</button>

这是我的按钮在javascript文件中使用的位置,该文​​件位于代码的第134行:https://jsfiddle.net/4hty6kak/16/

   _visualize: function(audioContext, buffer) {
    audioBufferSouceNode = audioContext.createBufferSource(),
        analyser = audioContext.createAnalyser(),
        that = this;
    //connect the source to the analyser
    audioBufferSouceNode.connect(analyser);

            jQuery('.play').click(function() {
                audioBufferSouceNode.start();   
            });

            jQuery('.pause').click(function() {
                audioBufferSouceNode.stop();
            });

            jQuery('.volumeMax').click(function() {
                audioBufferSouceNode.volume=1;
            });

            jQuery('.volumestop').click(function() {
            audioBufferSouceNode.volume=0;
            });

            jQuery('.playatTime').click(function() {
                audioBufferSouceNode.currentTime= 35;
                audioBufferSouceNode.play();
            }); 



    //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
    audioBufferSouceNode.buffer = buffer;
    //play the source
    if (!audioBufferSouceNode.start) {
        audioBufferSouceNode.start = audioBufferSouceNode.noteOn //in old browsers use noteOn method
        audioBufferSouceNode.stop = audioBufferSouceNode.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);
    }
    audioBufferSouceNode.start(0);
    this.status = 1;
    this.source = audioBufferSouceNode;
    audioBufferSouceNode.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/16/

我尝试过使用

audioBufferSouceNode.source.start(); 

&安培;

audioBufferSouceNode.source.stop(); 
相反,但仍然没有工作! 你有什么解决办法?按照这个速度,我不关心你有什么解决方案,即使它使用的是jQuery。简单明了,容易解决。

1 个答案:

答案 0 :(得分:1)

您只能在start上使用audioBufferSourceNode一次,对web audio API的大多数源节点使用_visualize。见http://webaudio.github.io/web-audio-api/#widl-AudioBufferSourceNode-start-void-double-when-double-offset-double-duration

  

start可能只被调用一次

因此,如果您需要这些行为,则需要更改逻辑,这意味着您必须在启动时重新创建节点。我做了一个几乎没有工作的版本,只是为了展示你如何工作,但正如我所说,如果你希望它能够工作,它需要对你的代码进行一些重要的重构。 但基本上,你应该有一个创建音频节点并进行连接的start方法。在您的情况下,_visualize并不太远,因此在示例中我在启动时调用了analyser。但显然它会导致其他问题,例如点击事件。 你必须看到你需要回收哪些对象,你可以保留一些(例如 _visualize: function (audioContext, buffer) { audioBufferSouceNode = audioContext.createBufferSource(); that = this; //connect the source to the analyser audioBufferSouceNode.connect(analyser); jQuery('.play').click(function () { audioContext.decodeAudioData(fileResult, function (buffer) { that._updateInfo('Decode succussfully,start the visualizer', true); that._visualize(audioContext, buffer); }, function (e) { that._updateInfo('!Fail to decode the file', false); console.log(e); }); }); ),但有些不是,所以你也必须对它进行排序。

mediaElementSourceNode

https://jsfiddle.net/h3yvp0zw/4/

再一次,它只是一个例子,它运行不正常,经过几次点击后,可视化工具因某种原因而下降,可能是点击事件过多。这只是为了说明问题所在。

编辑:

使用audioBufferSourceNode代替mediaElementSourceNode可能更好的方法。 HTML Audio Element允许将常规HTML Audio element提供给上下文,因此应用效果和/或分析器。这允许使用var audioElement = new Audio('sourcefile'); 的常规控件,例如播放和暂停。

IT以这种方式运作:

首先创建一个音频元素。

mediaElementSource

然后你的var source = audioContext.createMediaElementSource(audioElement); 引用你的元素:

connect

然后analyser将其发送到source.connect(analyser);

audioElement.play();
audioElement.pause();

并将您的行为应用于您的元素:

mediaElementSource

请注意,虽然audio元素可以使用跨域文件,但Uncaught TypeError: Cannot call method 'replace' of undefined angular.js:4407 不是这种情况。安全性是不同的,如果你在音频元素上使用跨域文件,音频元素可能会播放,但是一旦你将它提供给mediaSourceElement,它就会停止,它只会输出静音。

这意味着您需要修改文件的处理方式,您可能需要将它们放在服务器上,然后将路径提供给音频元素。