Hi everyone,

I am trying to make a toggling on/off button for a looping sound using AS3. Currently it starts with the on icon showing but when pressed shows the off icon but it must be pressed again to stop the music but then shows the on icon while the music is stopped and vice versa. I also have sound on my navigation buttons that are stopped when the stop icon for the loop is pressed. How can I keep the sounds for the navigation icons to continue to play and get the on/off icons for the loop working? Any help or links to a good tutorial would be great :)

var music:Sound = new Sound(new URLRequest("music.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;
      }

Dani AI

Generated

Two separate issues are visible in the thread: the loop’s on/off icons getting out of sync, and the site navigation sounds being silenced when the loop is turned off. Both stem from treating the loop as if it were a “global” sound. Using SoundMixer.soundTransform mutes everything. For a looping background track, keep a single SoundChannel reference for that track and control only that channel (or its SoundTransform). That keeps short UI/nav SFX independent and lets the icons reflect the actual playback state.

Example pattern (controls only the background loop; pause/resume supported):

var bgSound:Sound = new Sound(new URLRequest("music.mp3"));
var bgChannel:SoundChannel;
var bgTransform:SoundTransform = new SoundTransform(1, 0); // volume, pan
var isPlaying:Boolean = true;
var pausePos:Number = 0;

bgChannel = bgSound.play(0, 9999, bgTransform);

play_btn.addEventListener(MouseEvent.CLICK, toggleMusic);
stop_btn.addEventListener(MouseEvent.CLICK, toggleMusic);

function toggleMusic(e:MouseEvent):void {
  if (isPlaying) {
    pausePos = bgChannel ? bgChannel.position : 0;
    if (bgChannel) { bgChannel.stop(); bgChannel = null; }
    isPlaying = false;
    play_btn.visible = true;
    stop_btn.visible = false;
  } else {
    bgChannel = bgSound.play(pausePos, 9999, bgTransform);
    isPlaying = true;
    play_btn.visible = false;
    stop_btn.visible = true;
  }
}

Practical troubleshooting notes: verify instance names on the stage (match code exactly), ensure only one click listener per control, and place initial visibility/state setup on frame 1. If a button requires two clicks, check for overlapping movieclips or invisible hit areas (set mouseChildren = false on parent clips when appropriate). Also confirm the pan argument (second SoundTransform parameter) is 0 unless intentional; a -1 pan will send audio to the left channel.

This approach resolves the nav-sounds-muting problem and keeps the UI icons aligned with the actual playback state. It also addresses the scenario described and should work with the CS3 example file posted by @rajarajan07 as long as the file applies per-channel control rather than global SoundMixer changes.

Recommended Answers

All 4 Replies

Member Avatar for Member #334542

Please find the attachment

Thank you for responding so quickly rajarajan07! I tried to open the file but it said it was an unexpected file format. I am using CS3. Could you save it in that format? I have been searching and doing tutorials and am still stuck. I am really looking forward to see what you have done! Thank you again for your reply!

Member Avatar for Member #334542

Please find the attachment in CS3 version.

Hi rajarajan07

How is the code (AS3) when you start with the sound on first.

Like.. when you load the page the sound is playing and there is a sound off button . After clicking the sound stops and a play button appears (and vice versa).

You would really help me we with this.

Kind regards,
Bobby B

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.