Skip to main content
Inspiring
April 12, 2016
Answered

Play/Pause streaming audio button for each frame

  • April 12, 2016
  • 1 reply
  • 1632 views

I have streaming audio on each frame that automatically starts and stops when the next or previous frame is clicked. I need to add a pause button on each frame. I have created a play/pause toggle movie clip on my first frame and it works fine. Frame one uses mySound1 and myChannel1, frame two uses mySound2 and myChannel2, etc.

When I try to add the code to the next frame I get errors. What do I need to do to have a button on each frame to stop the corresponding mp3? I appreciate your help!

SoundMixer.stopAll();

var pP:Number = 0;//playback position

var playing:Boolean = false;

var mySound1:Sound = new Sound();

var myChannel1:SoundChannel = new SoundChannel();

mySound1.load(new URLRequest("Physiology Slide 01 INTRO.mp3"));

myChannel1 = mySound1.play();

SoundMixer.stopAll();

PlayPause_btn.addEventListener(MouseEvent.CLICK, togglePlay);

togglePlay(null);

    function togglePlay(event:MouseEvent):void {

        playing = !playing;

        if (playing) playS();

        else pauseS();

    }

    function playS() {

        myChannel1 = mySound1.play(pP);

        PlayPause_btn.gotoAndStop(2);

    }

    function pauseS() {

        pP = myChannel1.position;

        myChannel1.stop();

        PlayPause_btn.gotoAndStop(1);

    }

This topic has been closed for replies.
Correct answer kglad

are you using the same function names (ie, togglePlay, playS and pauseS) on your keyframes?  if so, you'll see a duplicate function error (1120, i think).

to remedy, use different function names or use a switch/case or if/else sequence in one of each function.

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
April 12, 2016

are you using the same function names (ie, togglePlay, playS and pauseS) on your keyframes?  if so, you'll see a duplicate function error (1120, i think).

to remedy, use different function names or use a switch/case or if/else sequence in one of each function.

Inspiring
April 12, 2016

I have this on the next frame but the button doesn't work.

SoundMixer.stopAll();

var mySound2:Sound = new Sound();

var myChannel2:SoundChannel = new SoundChannel();

mySound2.load(new URLRequest("Physiology Slide 02.mp3"));

myChannel2 = mySound2.play();

PlayPause_btn.addEventListener(MouseEvent.CLICK, togglePlay2);

togglePlay2(null);

    function togglePlay2(event:MouseEvent):void {

        playing = !playing;

        if (playing) playS();

        else pauseS();

    }

    function playS2() {

        myChannel2 = mySound2.play(pP);

        PlayPause_btn.gotoAndStop(2);

    }

    function pauseS2() {

        pP = myChannel2.position;

        myChannel2.stop();

        PlayPause_btn.gotoAndStop(1);

    }

kglad
Community Expert
Community Expert
April 12, 2016

you're using the same name for that button.

if that's in a frame 2 keyframe, give it a different name.  if it's the same button as the frame 1 button, remove the frame 1 listener.