简体   繁体   English

为soundjs添加暂停功能

[英]Add pause function to soundjs

I have tried using: 我尝试过使用:

  1. createjs.Sound.pause;

  2. mySound.stop();

Nothing works. 什么都行不通。 Right now it have a stop button but if the stop button is pressed then the sound would not be played again. 现在它有一个停止按钮,但如果按下停止按钮,则不会再次播放声音。

This is the soundjs that im using. 这是我正在使用的声音。 soundjs soundjs

My code is exactly the same with the source code of it. 我的代码与它的源代码完全相同。 Thanks for any help! 谢谢你的帮助!

I end up doing it like this Based on Stephen Lightcap answer: 我最终这样做基于Stephen Lightcap答案:

Javascript inside the head tag: 头标记内的Javascript:

    function load() {
        // Update the UI
        document.getElementById("display").innerText = ".";


        // Load the sound
        createjs.Sound.alternateExtensions = ["mp3"];
        createjs.Sound.addEventListener("fileload", handleFileLoad);
        createjs.Sound.registerSound({id:"mySound", src:"../../test.mp3"});
    }

    function handleFileLoad(event) {
        // Update the UI
        document.getElementById("display").innerHTML = ".";
        document.getElementById("stopBtn").disabled = "";

        // Play the loaded sound
        createjs.Sound.play(event.src,{loop:2});

    }
    var instance = createjs.Sound.play(event.src);
</script>

Button inside the body tag: body标签内的按钮:

<input id="pausBtn" type="button" value="Pause" onclick="pause()"/>

Javascript for the button onclick. 按钮onclick的Javascript。 Placed below the button. 放在按钮下方。 :

<script>
function pause(){
instance.pause();
}
</script>

But I got an error saying : 但我得到一个错误说:

Uncaught TypeError: Cannot read property 'pause' of undefined at pause (phkk.html:560) at HTMLInputElement.onclick (phkk.html:177) 未捕获的TypeError:无法在HTMLInputElement.onclick(phkk.html:177)中读取暂停时未定义的属性'pause'(phkk.html:560)

You can store it in an var and then use the function pause() 您可以将其存储在var中,然后使用函数pause()

var instance = createjs.Sound.play(event.src);

Have button that calls a method that will store the pause 具有调用将存储暂停的方法的按钮

<input id="pausBtn" type="button" value="Pause" onclick="pause()"/>

The JS function: JS功能:

function pause(){
    instance.pause();
}

EDIT 编辑

I went over the docs and it appears it doesn't function like that any more, so you're going to have to use the paused property 我查看了文档,看起来它不再具有这样的功能了,所以你将不得不使用暂停的属性

Here's a rough outline on how it works for me: 这是一个关于它如何为我工作的大致轮廓:

<script>

    var instance; 

    function load() {
        // Update the UI
        document.getElementById("display").innerText = ".";


        // Load the sound
        createjs.Sound.alternateExtensions = ["mp3"];
        createjs.Sound.addEventListener("fileload", handleFileLoad);
        createjs.Sound.registerSound({id:"mySound", src:"../../test.mp3"});
    }

   function handleFileLoad(event) {
       // Update the UI
       document.getElementById("display").innerHTML = ".";
       document.getElementById("stopBtn").disabled = "";

       // Play the loaded sound
       instance = createjs.Sound.play(event.src,{loop:2});

   }

   function pause(){
       instance.paused ? instance.paused = false : instance.paused = true;
   }



</script>

The HTML buttons: HTML按钮:

<body>
<input id="loadBtn" type="button" value="Begin Loading" onclick="load()"/>
<input id="stopBtn" type="button" value="Stop Playing" onclick="createjs.Sound.stop();" disabled="disabled" />
    <input id="pauseBtn" type="button" value="Pause" onclick="pause()"/>
<label id="display">Waiting for User Input. Click "Begin Loading".</label>

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

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