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?

Recommended Answers

All 13 Replies

Member Avatar for rajarajan2017

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

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.

Member Avatar for rajarajan2017

var channel:SoundChannel = music.play(0, 1, trans);

Use this channel when you need to play.

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.

Member Avatar for rajarajan2017
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;
}

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.

Member Avatar for rajarajan2017

I couldn't received any mail from you

Member Avatar for rajarajan2017

Please check it and give me the feedback

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.

Member Avatar for rajarajan2017

thanks man! COOL! Don't hesitate to ask any help!

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

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;
      }
Member Avatar for rajarajan2017

Please disable the button when the music stops, keep enabled when playing.

Note: Please don't continue with the solved threads.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.