Skip to main content
Participating Frequently
September 8, 2017
Answered

Play/pause audio in timeline

  • September 8, 2017
  • 6 replies
  • 6197 views

Hi to all,

I try to create a play/pause buttons for to play/pause an animation on the timeline, the buttons work well for the animation, but noto for the audio... this is because in the canvas sets it isn't possible use audio in streaming but only in event mode...

I  find documentation only for external audio but not in timeline

this is my code inside a MC playpause_mc

this.stop_btn.addEventListener("click", stop_anim.bind(this));

function stop_anim() {

//javascript:alert("Mouse clicked");

this.parent.stop();

this.gotoAndStop("STOP");

//stop audio in parent

}

Can you help me?

This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer Colin Holgate

You can stop all sound with this:

createjs.Sound.stop();

That would work for your example code, but if you really wanted to pause a song and then continue from where you left off, or from another position in the sound, it's slightly harder.

You would need to play the sound with code, and then you can pause the sound, set its position, or unpause the sound. Here's an example, where the first script is in frame 1, and the second is in frame 48, and in the library is a sound with its linkage set to be 'tune':

//frame 1

window.pos = 0;

window.snd = createjs.Sound.play("tune");

//frame 48

this.stop();

window.pos = window.snd.position;

window.snd.paused = true;

setTimeout(playsnd,3000);

function playsnd(){

  window.snd.position = window.pos + 3000;

  window.snd.paused = false;

}

Those scripts would set a sound going, then pause it after two seconds (in a 24 fps FLA), and in 3 seconds it would advance the sound by 3 seconds and unpause it.

The 'window.' part isn't needed, but I put it there to make it clear that the snd and pos variables are global.

6 replies

Colin Holgate
Colin HolgateCorrect answer
Inspiring
September 11, 2017

You can stop all sound with this:

createjs.Sound.stop();

That would work for your example code, but if you really wanted to pause a song and then continue from where you left off, or from another position in the sound, it's slightly harder.

You would need to play the sound with code, and then you can pause the sound, set its position, or unpause the sound. Here's an example, where the first script is in frame 1, and the second is in frame 48, and in the library is a sound with its linkage set to be 'tune':

//frame 1

window.pos = 0;

window.snd = createjs.Sound.play("tune");

//frame 48

this.stop();

window.pos = window.snd.position;

window.snd.paused = true;

setTimeout(playsnd,3000);

function playsnd(){

  window.snd.position = window.pos + 3000;

  window.snd.paused = false;

}

Those scripts would set a sound going, then pause it after two seconds (in a 24 fps FLA), and in 3 seconds it would advance the sound by 3 seconds and unpause it.

The 'window.' part isn't needed, but I put it there to make it clear that the snd and pos variables are global.

Participating Frequently
September 22, 2017

Thanks Collin,

I try but the play doesn't run... I'm sure I'm making some mistakes...

window.pos = 0;

window.snd = createjs.Sound.play("tune");

this.stop_btn.addEventListener("click", videoStop.bind(this));

this.stop_btn.addEventListener("click", videoPlay.bind(this));

function videoStop() {

this.stop();

window.pos = window.snd.position;

window.snd.paused = true;

}

function videoPlay() {

this.play();

window.snd.position = window.pos;

window.snd.paused = false;

}

Colin Holgate
Inspiring
September 22, 2017

You may need to make the initial play of the sound be from a button, otherwise the sound hasn't been played from a user action, which it needs to be. Like:

this.stop();

this.start.addEventListener("click",firstPlay.bind(this));

this.stop_btn.addEventListener("click", videoStop.bind(this));

this.stop_btn.addEventListener("click", videoPlay.bind(this));

function firstPlay(){

   window.pos = 0;

   window.snd = createjs.Sound.play("tune");

}

function videoStop() {

this.stop();

window.pos = window.snd.position;

window.snd.paused = true;

}

function videoPlay() {

this.play();

window.snd.position = window.pos;

window.snd.paused = false;

}

Preran
Legend
September 8, 2017
Colin Holgate
Inspiring
September 8, 2017

They are using Canvas, so the AS3 solutions won't apply.

Preran
Legend
September 11, 2017

Thank  you for the clarification, Colin. I was not careful enough in my reading.

Do you have any solutions for this user?

Thanks,

Preran