Skip to main content
Participant
August 13, 2020
Question

Adding music-Please Help!

  • August 13, 2020
  • 1 reply
  • 152 views

Let me preface this...I am a student learning Animate. I have knowledge of Photoshop and Illustrator previous to going to school. (Just want you to know what my limitations might be).  

 

I am making a short "game". I am using scenes. I want to add a music button to each level that plays the music automatically but has a start/stop feature. I was trying to use this code - 

 

/* Click to Play/Stop Sound
Clicking on the symbol instance plays the specified sound.
Clicking on the symbol instance a second time stops the sound.

Instructions:
1. Replace "http://www.helpexamples.com/flash/sound/song1.mp3" below with the desired URL address of your sound file. Keep the quotation marks ("").
*/

instance_name_here.addEventListener(MouseEvent.CLICK, fl_ClickToPlayStopSound);

var fl_SC:SoundChannel;

//This variable keeps track of whether you want to play or stop the sound
var fl_ToPlay:Boolean = true;

function fl_ClickToPlayStopSound(evt:MouseEvent):void
{
                if(fl_ToPlay)
                {
                                var s:Sound = new Sound(new URLRequest("http://www.helpexamples.com/flash/sound/song1.mp3"));
                                fl_SC = s.play();
                }
                else
                {
                                fl_SC.stop();
                }
                fl_ToPlay = !fl_ToPlay;
}

 I found the song that I want but it was a download from a free site. How do I convert that to a url? Any ideas? Thanks in advance for any help.

Tracey

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
August 14, 2020

you need to download the sound first and then (when download is complete), play it (but that might cause a cross-domain security issue).

 

so you should download it and then either play it locally, or import it to your library and play it.