Create an on/off sound button?
ahah it's me again. I'm going to make an on button and an off button which i want to play an audio file and pause it. But what code do i use to do this?
snitch321
Junior Poster in Training
63 posts since Nov 2007
Reputation Points: 9
Solved Threads: 2
Just check with the code
var music:Sound = new Sound(new URLRequest("music.mp3"));
var trans:SoundTransform = new SoundTransform(1, -1);
var channel:SoundChannel = music.play(0, 1, trans);
var musicOn:Boolean = true
onBtn.addEventListener(MouseEvent.CLICK, onSound);
offBtn.addEventListener(MouseEvent.CLICK, offSound);
function offSound(e:Event) {
musicOn = false;
trans.volume=0;
SoundMixer.soundTransform = trans;
}
function onSound(e:Event) {
musicOn = true;
trans.volume=1;
SoundMixer.soundTransform = trans;
}
rajarajan07
Nearly a Posting Virtuoso
1,447 posts since May 2008
Reputation Points: 167
Solved Threads: 239
ah thanks raj, again the legend you are has arisen to the challenge with success. i thank you very much, and appreciate your attention to my problem.
Btw is there a way to stop the mp3 from automatically playing when you enter that frame.
snitch321
Junior Poster in Training
63 posts since Nov 2007
Reputation Points: 9
Solved Threads: 2
var channel:SoundChannel = music.play(0, 1, trans);
Use this channel when you need to play.
rajarajan07
Nearly a Posting Virtuoso
1,447 posts since May 2008
Reputation Points: 167
Solved Threads: 239
if you mean replace that line with the new on you suggested then it doesnt work as its the same? but if you mean adapt that line then im not sure how, to stop the music automatically.
snitch321
Junior Poster in Training
63 posts since Nov 2007
Reputation Points: 9
Solved Threads: 2
var music:Sound = new Sound(new URLRequest("music.mp3"));
var trans:SoundTransform = new SoundTransform(1, -1);
var channel:SoundChannel;
music.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
var musicOn:Boolean;
playBtn.addEventListener(MouseEvent.CLICK,playfn);
onBtn.addEventListener(MouseEvent.CLICK, onSound);
offBtn.addEventListener(MouseEvent.CLICK, offSound);
function onComplete(evt:Event):void {
//Stop loaded sound
channel = music.stop();
}
function playfn(e:MouseEvent):void{
channel= music.play(0, 1, trans);
musicOn=true;
}
function offSound(e:Event) {
musicOn = false;
trans.volume=0;
SoundMixer.soundTransform = trans;
}
function onSound(e:Event) {
musicOn = true;
trans.volume=1;
SoundMixer.soundTransform = trans;
}
rajarajan07
Nearly a Posting Virtuoso
1,447 posts since May 2008
Reputation Points: 167
Solved Threads: 239
I've in putted your new code yet there is problems to which im not sure of. i have attached the file here.
Btw, the 'home' button is the on button and the 'last page' is the off button.
Ill have to send to you by email due to a problem.
snitch321
Junior Poster in Training
63 posts since Nov 2007
Reputation Points: 9
Solved Threads: 2
I couldn't received any mail from you
rajarajan07
Nearly a Posting Virtuoso
1,447 posts since May 2008
Reputation Points: 167
Solved Threads: 239
Please check it and give me the feedback
rajarajan07
Nearly a Posting Virtuoso
1,447 posts since May 2008
Reputation Points: 167
Solved Threads: 239
Cheers man for the adapted version you sent me. i wanted just 2 buttons on and off, so i mucked around a bit and manage to attain just that. I made the 'Play' button code refer to the onBtn and that way i had the on button doing both jobs of beginning the music and playing after pausing it.
thank you m8. If you want a program just ask m8. i owe you one.
snitch321
Junior Poster in Training
63 posts since Nov 2007
Reputation Points: 9
Solved Threads: 2
thanks man! COOL! Don't hesitate to ask any help!
rajarajan07
Nearly a Posting Virtuoso
1,447 posts since May 2008
Reputation Points: 167
Solved Threads: 239
Please disable the button when the music stops, keep enabled when playing.
Note: Please don't continue with the solved threads.
rajarajan07
Nearly a Posting Virtuoso
1,447 posts since May 2008
Reputation Points: 167
Solved Threads: 239