Link to home
Start Free TrialLog in
Avatar of Karl01
Karl01

asked on

Flash buton to both start and stop music

Hi,

I currently have a project with two buttons to play and stop a music track, however I wish to have just one button to both start and stop the music but without the problem of playing a new channel over another creating an echo effect, I think this will need a Boolean and an ‘if statement’ to check if the sound is on or off however I have not manage to get any working. N.B. I may be way over complicating things in my second lot of code from an online tutorial.

I have this code for the two buttons:

import flash.events.MouseEvent;
stop();
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0;
mySound.load(new URLRequest("Welcome_to_the_Jungle.mp3"));

Play1.addEventListener(MouseEvent.CLICK, onClickPlay);

function onClickPlay(e:MouseEvent):void{
myChannel = mySound.play(lastPosition);
}

Stop1.addEventListener(MouseEvent.CLICK, onClickStop);

function onClickStop(e:MouseEvent):void{
myChannel.stop();
}


This uses a Play1 and Stop1 button but i would like to have just one on/off button.


I have been trying to get this code to work from an online tutorial:

import flash.media.Sound;
import flash.media.SoundChannel;

var soundOn:Boolean = true; //music is ON when we start
var myMusic:TitleMusic = new TitleMusic(new URLRequest("Welcome_to_the_Jungle.mp3"));
var myChannel:SoundChannel = myMusic.play(0,1000); // endless loop, in effect
var myTransform:SoundTransform;

PlayStop.addEventListener(MouseEvent.CLICK,toggleSound);
PlayStop.buttonMode = true;
PlayStop.mouseChildren = false;


function toggleSound(e:MouseEvent)
{
    if(soundOn)
    {
        // turn sound off
        myTransform = new SoundTransform();
        myTransform.volume = 0; // silent
        myChannel.soundTransform = myTransform;
        soundOn = false;
        PlayStop.myButtonText.text = "click to turn sound ON";
    }
    else // sound is off
    {
        // turn sound on
        myTransform = new SoundTransform();
        myTransform.volume = 1; // full volume
        myChannel.soundTransform = myTransform;
        soundOn = true;
        PlayStop.myButtonText.text = "click to turn sound OFF";
    }
   
}

However I keep getting errors such as:

1046: Type was not found or was not a compile-time constant: TitleMusic.


Thank for any help

Karl
Avatar of gingermoleman
gingermoleman

hI,

You could just create a  switch and control it yourself

var soundplay: String = 0;

then in the button function

if(soundplay==0){
play your sound, set btn txt, etc
end with
soundplay =1;

}
else{
stop your sound, set btn txt, etx
soundplay=0;
}
sorry, surround all the strings with " "

var soundplay: String ="0";

then in the button function

if(soundplay=="0"){
play your sound, set btn txt, etc
end with
soundplay ="1";

}
else{
stop your sound, set btn txt, etx
soundplay="0";
}
Avatar of Karl01

ASKER

Hi gingermoleman,

I have tried your code as follows but I keep getting an error as show below, any ideas on what might be causing this, it does not seam to like the way I am trying to stop the music.


import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
stop();
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0;
var soundplay: String ="0";
mySound.load(new URLRequest("Welcome_to_the_Jungle.mp3"));

Play1.addEventListener(MouseEvent.CLICK, onClickPlay);

function onClickPlay(e:MouseEvent):void {

      if (soundplay=="0") {
            myChannel = mySound.play(lastPosition);
            soundplay ="1";

      } else {
            myChannel = mySound.stop();
            soundplay="0";
      }
}


Error:
Line 19 –
061: Call to a possibly undefined method stop through a reference with static type flash.media:Sound.

Thank you for your help.

Karl
Avatar of Karl01

ASKER

Sorry its error code 1061 and line 19 is:

myChannel = mySound.stop();

Regards
ASKER CERTIFIED SOLUTION
Avatar of gingermoleman
gingermoleman

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Karl01

ASKER

That code work great, thank you very much.

Karl
extra pointer, probably best to set soundplay to values like "playing" and "paused" instead of  "0" and "1". Just makes its easier to read at a later point