CreateJS / SoundJS

A Javascript library for working with Audio. It provides a consistent API for loading and playing audio on different browsers and devices. Currently supports WebAudio, HTML5 Audio, Cordova / PhoneGap, and a Flash fallback.
http://createjs.com/
MIT License
4.42k stars 838 forks source link

Pause not toggling for a single mp3 #253

Open JarlAlverson opened 7 years ago

JarlAlverson commented 7 years ago

I have code in 60+ files to play an mp3, and pause it on an event handler. All the files but one only play a single mp3 that would need to be paused. One file has three. My pause code works on all the files with a single mp3. The file with the three mp3s my pause code does not work on one out of three of them.

The mp3s are being assigned id's as such:

manifest: [
    {src:"../sounds1/cwav.mp3?1472678838595", id:"1"},
    {src:"../sounds1/dwav.mp3?1472678838595", id:"2"},
    {src:"../sounds1/rwav.mp3?1472678838595", id:"3"}
]

The various audio files are called with the following:

this.frame_0 = function() {
    playSound("1");
}
this.frame_122 = function() {
    this.stop();
    this.button_1.addEventListener("click", fl_ClickToGoToAndPlayFromFrame.bind(this));
    function fl_ClickToGoToAndPlayFromFrame()
    {
        this.gotoAndPlay(123);
    }
}
this.frame_123 = function() {
    this.return_btn.addEventListener("click", fl_ClickToGoToAndStopAtFrame.bind(this));
    function fl_ClickToGoToAndStopAtFrame()
    {
        this.gotoAndStop(122);
    }
}
this.frame_158 = function() {
    playSound("2");
}
this.frame_253 = function() {
    playSound("3");
}
this.frame_318 = function() {
    this.stop();
}

The code for pausing the play back is:

var PlayPos = 0;
var thissound = '';
function playSound(id, loop) {
    document.getElementById("debug").innerHTML = 'cleared';
    if(isNaN(id)) {
        return createjs.Sound.play(id);
    } else {
        document.getElementById('Pause').style.cursor='pointer';
        document.getElementById('Pause').addEventListener("mouseover",function() {document.getElementById('Pause').src='../images/navOver_02.png';});
        document.getElementById('Pause').addEventListener("mousedown",function(){ pauseSound(id) });
        thissound=createjs.Sound.play(id);
    }
}
function pauseSound(id) {
    if(thissound.paused){
        thissound.seek=PlayPos;
        thissound.paused=false;
    document.getElementById("debug").innerHTML = 'event heard paused true '+id+' '+thissound.paused;
        document.getElementById('Pause').addEventListener("mouseout",function() {document.getElementById('Pause').src='../images/nav_02.png';});
        document.getElementById('Pause').addEventListener("mousedown",function() {document.getElementById('Pause').src='../images/navClick_02.png';});
        document.getElementById('Pause').addEventListener("mouseup",function() {document.getElementById('Pause').src='../images/navOver_02.png';});
    } else {
        thissound.paused=true;
    document.getElementById("debug").innerHTML = 'event heard paused false '+id+' '+thissound.paused;
        PlayPos = thissound.getPosition();
        document.getElementById('Pause').addEventListener("mouseout",function() {document.getElementById('Pause').src='../images/navClick_02.png';});
        document.getElementById('Pause').addEventListener("mouseup",function() {document.getElementById('Pause').src='../images/navClick_02.png';});
    }
}

The audio file that does not pause with this code is id 2. Im doing the isNAN check to determine if the audio file being played is one that would need to be paused. The innerhtml for the debug id is me trying to figure out the issue. With audio file id 2 the event listener does trigger properly, but the paused parameter will not register in the if statement. In other words document.getElementById("debug").innerHTML = 'event heard paused true '+id+' '+thissound.paused; displays "event heard paused true 2 false". So even though is displays as false, it still qualifies as true in the if after being set to false with repeated clicks.

To reiterate, this code works fine on all my other files and on id 1 and 3 in this file. Does anyone have any insights into why this may be, or can anyone spot some error in my code I am missing from proof reading my own paper?

Thank you in advance.