954,598 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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
 

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;
}


Hello rajarajan07,

I am new to actionscript 3 and am trying to add a sound on icon and sound off icon that toggle the icons back and forth when playing and pausing sound. I found your code and it is working perfectly but with both icons sitting on the stage at the same time. Is it possible to have the icons toggle? Thank you in advance if you have the time to respond.

Here is what I am currently using:

var music:Sound = new Sound(new URLRequest("music.mp3"));
var trans:SoundTransform = new SoundTransform(1, -1);
var channel:SoundChannel = music.play(0,1000);
var musicOn:Boolean = true
onBtn.addEventListener(MouseEvent.CLICK, onSound);
onBtn.buttonMode = true;
onBtn.mouseChildren = false;
offBtn.addEventListener(MouseEvent.CLICK, offSound);
offBtn.buttonMode = true;
offBtn.mouseChildren = false;
function offSound(e:Event) {
   musicOn = false;
        trans.volume=0;
        SoundMixer.soundTransform = trans;
}  
function onSound(e:Event) {
        musicOn = true;
        trans.volume=1;
        SoundMixer.soundTransform = trans;
}
BlueCarNumber9
Newbie Poster
7 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

Hello again,

I changed a few things and it is getting closer and is mostly working. The only problem is the play button is pressed once and the stop button appears and must be pressed again before the music stops. I'm not sure this is the best way to do it:

var music:Sound = new Sound(new URLRequest("toepianoloop.mp3"));
var trans:SoundTransform = new SoundTransform(1, -1);
var channel:SoundChannel = music.play(0, 1000, trans);
var musicOn:Boolean = true
      play_btn.addEventListener(MouseEvent.CLICK, onSound);
      stop_btn.addEventListener(MouseEvent.CLICK, offSound);
	  stop_btn.visible = false;
      
	  function offSound(e:Event) {
      musicOn = false;
      play_btn.visible = true;
	  stop_btn.visible = false;
	  trans.volume=0;
      SoundMixer.soundTransform = trans;
      }
      
	  function onSound(e:Event) {
      musicOn = true;
	  play_btn.visible = false;
	  stop_btn.visible = true;
      trans.volume=1;
      SoundMixer.soundTransform = trans;
      }
BlueCarNumber9
Newbie Poster
7 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: