Can anyone help me I cant seem to get my play/stop button to stop and play my background music.
i imported my sound to my library and created the button to play and stop the sound but it doesnt work
what is the easiest way to do this in action script 3.0?
i tried uploading my file but i cant

Dani AI

Generated

gave a working timeline/AS2-style answer, but that code and the attachSound/on(release) approach will not work in an ActionScript 3.0 FLA. In AS3 you use the flash.media.Sound and flash.media.SoundChannel classes and event listeners on button instances. Make sure the FLA is published as ActionScript 3.0 and that the buttons on stage have instance names (for example play_btn and stop_btn).

A minimal AS3 pattern for a library-exported sound (set a linkage class name in the Library, e.g. BgMusic) is:

import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.MouseEvent;

var bg:Sound = new BgMusic(); // class name from Library linkage
var channel:SoundChannel;
var pausedAt:Number = 0;

play_btn.addEventListener(MouseEvent.CLICK, onPlay);
stop_btn.addEventListener(MouseEvent.CLICK, onStop);

function onPlay(e:MouseEvent):void {
  channel = bg.play(pausedAt, int.MAX_VALUE); // loops indefinitely
  pausedAt = 0;
}

function onStop(e:MouseEvent):void {
  if (channel) {
    pausedAt = channel.position; // remember position for resume
    channel.stop();
  }
}

Troubleshooting tips: confirm the library item is exported for ActionScript and the class name matches your code; place the code in a frame after the buttons exist (or use a document class); use SoundChannel.stop() to stop playback (not Sound); and be careful with preloaders — exporting large audio into the first frame can break them. Also note that Flash Player is deprecated in modern browsers; for web delivery today consider converting audio controls to HTML5 audio or using a Flash-to-HTML5 migration strategy if the SWF must be preserved.

Recommended Answers

All 2 Replies

This might work.

  1. Name your sound file mix1 in the Linkage menu, and select the "Export for Actionscript" and "Export in first frame" options
  2. Make 3 consecutive key frames in the movie timeline
  3. Make your play button the second frame (label as "play"), and make your stop button the last frame (label as "stop")

Paste

my_sound = new Sound();
my_sound.attachSound("mix1");
play();

into the actions panel for the first key frame. Give the stop() action to the second and third key frames. Paste

on (release) {
_root.my_sound.start(0,1000);
_root.gotoAndStop("stop");
}

into the actions panel for the play button in the second key frame. Finally, paste

on (release) {
_root.my_sound.stop();
_root.gotoAndStop("play");
}

into the actions panel for the stop button in the third key frame.

If you want to start with the bg music already playing, use the following code for the first key frame instead of the code I gave at the beginning:

my_sound = new Sound();
my_sound.attachSound("mix1");
my_sound.start(0,1000);
gotoAndStop("stop");

That should work. Cheers!

This might work.

  1. Name your sound file mix1 in the Linkage menu, and select the "Export for Actionscript" and "Export in first frame" options
  2. Make 3 consecutive key frames in the movie timeline
  3. Make your play button the second frame (label as "play"), and make your stop button the last frame (label as "stop")

Paste

my_sound = new Sound();
my_sound.attachSound("mix1");
play();

into the actions panel for the first key frame. Give the stop() action to the second and third key frames. Paste

on (release) {
_root.my_sound.start(0,1000);
_root.gotoAndStop("stop");
}

into the actions panel for the play button in the second key frame. Finally, paste

on (release) {
_root.my_sound.stop();
_root.gotoAndStop("play");
}

into the actions panel for the stop button in the third key frame.

If you want to start with the bg music already playing, use the following code for the first key frame instead of the code I gave at the beginning:

my_sound = new Sound();
my_sound.attachSound("mix1");
my_sound.start(0,1000);
gotoAndStop("stop");

That should work. Cheers!

Thank you

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.