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?

Dani AI

Generated

Short summary and fixes based on the thread: wanted simple on/off controls for an MP3; gave good direction with Sound and SoundTransform, and ran into a UI/toggle timing problem. Two practical points were missed in the replies that commonly cause confusion — how to stop/pause correctly in AS3, and how to keep the UI consistent with the actual playback state.

Key technical corrections (concise)

  • A Sound instance does not have stop(). sound.play() returns a SoundChannel; call channel.stop() to stop that playback. If you want true pause/resume, save channel.position before calling channel.stop(), then call sound.play(savedPosition) to resume.
  • Toggling volume with a global SoundTransform (via SoundMixer.soundTransform) mutes everything in the player; use that only if you want to mute all sounds. For a single track, control the returned SoundChannel or use a per-play SoundTransform.

Why the “press once, stop later” issue happens

  • It usually stems from the sound already playing on load (autoplay) while the UI shows the opposite state. Either remove the initial play() so playback only starts on the On button, or if you must autoplay, set the UI to match (show the Stop button). Creating multiple channels by calling play() repeatedly without stopping the previous channel also leads to confusing behavior.

Practical checklist before posting changes

  • Declare channel at timeline scope so every handler can access it.
  • Keep a savedPosition number for pause/resume.
  • Update button visibility/enabled state immediately after changing audio state so UI and audio never disagree.
  • Persist mute preference with a SharedObject if you want the setting to survive reloads.
  • If loading remote MP3s, test in the debug player and watch for sandbox/crossdomain issues.

These steps fix the autoplay/toggle problems discussed and avoid the music.stop() miscall seen in post #6.

Recommended Answers

All 13 Replies

Member Avatar for Member #334542

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 Member #334542

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 Member #334542
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 Member #334542

I couldn't received any mail from you

Member Avatar for Member #334542

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 Member #334542

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 Member #334542

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.