Skip to main content
Known Participant
February 10, 2013
Answered

Sound problem code

  • February 10, 2013
  • 1 reply
  • 424 views

Hi. Everytime I open my swf file, the sound is playing. How can I get the sound to not play from the very start of the swf file?Here's the code I got from google. Thanks in advance! This is action script 3.0 by the way.

import flash.net.URLRequest;

import flash.media.Sound;

import flash.events.Event;

import flash.events.MouseEvent;

stop();

var req:URLRequest = new URLRequest("bgmusic.mp3");

var isPlaying:Boolean = true;

var snd:Sound = new Sound(req);

var channel:SoundChannel;

var pausePosition:Number = 0;

channel = snd.play(pausePosition);

playBtn.gotoAndStop(2);

playBtn.addEventListener(MouseEvent.CLICK, playPause);

channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);

function onPlaybackComplete(event:Event) {

          pausePosition = 0;

          channel = snd.play(pausePosition);

          isPlaying = true;

          channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);

}

function playPause(event:MouseEvent):void {

 

          if (isPlaying == false) {

                    channel = snd.play(pausePosition);

                    isPlaying = true;

                    playBtn.gotoAndStop(2);

                    channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);

          }

else {

          pausePosition = channel.position;

          channel.stop();

          playBtn.gotoAndStop(1);

          isPlaying = false;

}

}

This topic has been closed for replies.
Correct answer Ned Murphy

Try changing the following lines as indicated....

var isPlaying:Boolean = false;

...

channel = snd.play(pausePosition);  // remove these two lines

playBtn.gotoAndStop(2);

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
February 10, 2013

Try changing the following lines as indicated....

var isPlaying:Boolean = false;

...

channel = snd.play(pausePosition);  // remove these two lines

playBtn.gotoAndStop(2);