fcOnTheWeb Logo Web technologies, made easy.

fcOnTheWeb Red DotLoad sound with ActionScript

Today we look at how to load external sounds with ActionScript, and how to play.

The concept is fairly straight forward, so we will dive straight into it.

ActionScript 2

So basically, what we're going to do is to load in an MP3 that resides in the same folder as our SWF and play it when we click a button.

Let's start by taking a look at the end product, then we'll take a look at the ActionScript. Clicking the button below will play the MP3 we have loaded in.

Your version of Flash Player is lower than the required 10.0.0.

Click below to upgrade to the latest version and then try again.

Get Adobe Flash player

So that's nice and easy. Let's take a look at the simple ActionScript we need to make it work.

var my_sound:Sound = new Sound();

my_sound.loadSound("intro.mp3", false);

btn_sound.onRelease = function()

stopAllSounds();

my_sound.start();

}

So what are we doing here? Well, first we declare a new sound object, my_sound.

Next we load the sound we want - in this case our intro.mp3. We could have used a longer path here if we needed, but in this case we don't need to as the MP3 is in the same directory as our web page and SWF. The false is for the streaming property which designates whether the sound can start playing before it's fully loaded. As our sound is so small, and we activate it on button click, we don't need this functionality.

Then we simply set up our button click. First we call stopAllSounds(); which stops any sounds already playing. This won't stop any sounds embedded on the timeline, but it does stop any sounds started with ActionScript. We do this so multiple instances of the sound don't play over the top of each other if you press the button repeatedly.

And finally we start the sound playing with the start() method which plays the sound.

And it's as simple as that. There are quite a few other methods that can be called on a Sound object that we haven't addressed here, but this is just a simple introduction.

One important thing to note in conclusion is that larger sounds should be preloaded so they play when they're supposed to. They can be preloaded at the same time as the SWF is loaded, or they can have separate preloaders as they have the same getBytesLoaded() etc. methods as MovieClips. In our instance today we haven't preloaded the sound because it is so small (under 50KB) and will be loaded almost instantly on fast connections.

As always, you can download the source files for this example here: sound_as2.zip.

ActionScript 3

So basically, what we're going to do is to load in an MP3 that resides in the same folder as our SWF and play it when we click a button.

Let's start by taking a look at the end product, then we'll take a look at the ActionScript. Clicking the button below will play the MP3 we have loaded in.

Your version of Flash Player is lower than the required 10.0.0.

Click below to upgrade to the latest version and then try again.

Get Adobe Flash player

So that's nice and easy. Let's take a look at the simple ActionScript we need to make it work.

var sound_req:URLRequest = new URLRequest("intro.mp3");

var my_sound:Sound = new Sound();

my_sound.load(sound_req);

function play_sound(evt:MouseEvent):void {

SoundMixer.stopAll();

my_sound.play();

}

btn_sound.addEventListener(MouseEvent.MOUSE_UP, play_sound);

So what are we doing here? Well, first we declare a new URLRequest object, sound_req, with the path to the MP3 we want to load in. We could have used a longer path here if we needed, but in this case we don't need to as the MP3 is in the same directory as our web page and SWF.

Then we declare a new sound object, my_sound.

Next we load the sound.

Then we simply set up our button click. First we call stopAll(); which stops any sounds already playing. This won't stop any sounds embedded on the timeline, but it does stop any sounds started with ActionScript. We do this so multiple instances of the sound don't play over the top of each other if you press the button repeatedly.

And finally we start the sound playing with the play() method which plays the sound.

And it's as simple as that. There are quite a few other methods that can be called on a Sound object that we haven't addressed here, but this is just a simple introduction.

One important thing to note in conclusion is that larger sounds should be preloaded so they play when they're supposed to. They can be preloaded at the same time as the SWF is loaded, or they can have separate preloaders as they have the same getBytesLoaded() etc. methods as MovieClips. In our instance today we haven't preloaded the sound because it is so small (under 50KB) and will be loaded almost instantly on fast connections.

As always, you can download the source files for this example here: sound_as3.zip.

ferrari_chris

mel_06 says:

2010-01-27 09:18:56

that's nice! how about if i want to loadsound from this php code:

<?php echo base_url(); ?>uploads/<?php echo $artist_id; ?>/music/<?php echo $filename; ?>

how can i do this?

__________

chris (http://www.fcOnTheWeb.com) says:

2010-01-28 05:01:30

Good question.

Unfortunately we can't put the PHP you have there directly into ActionScript, but if you combine it with the ideas I've covered in the "FlashVars - Getting external content into Flash" article (http://fcOnTheWeb.com/articles/flashvars/) you can use PHP to print out the names of the files you want, them pass them into the SWF to have them loaded.

Good luck!

__________

Jim Sundberg says:

2010-12-02 17:20:16

Can this work with different images and sounds that are loaded in from FlashVars? I am trying to create a single MP3 player that can play different sounds by clicking on various photos spread around on the web page. I have been searching for tutorials for a couple of days and not found anything yet. I think it might involve passing the variables from JavaScript onClick back to the flash player with FlashVars. Any thoughts on this?

__________

Chris (http://www.fcOnTheWeb.com) says:

2010-12-05 09:45:55

Jim: I don't think JavaScript's required here, unless you want to update the FlashVars dynamically but I don't get that impression from your comment.

You can pass the MP3s (or their paths) into the SWF at run time using FlashVars printed with PHP.

I think what you need to do is have all of the MP3s loaded in the SWF, than called to play from a JavaScript event triggered by the HTML image.

__________

Cheap Ralph Lauren UK (http://cheapralphlaurenuk.1minutesite.co.uk/) says:

2014-01-18 03:05:10

Gucci Shoes UK, http://www.guccivshoesfactory.net/
Polo Ralph Lauren Outlet Online, http://www.rralphlaurenoutlet.com/
Ralph Lauren Outlet Online, http://www.ralphlaurenipolo.com/
Ralph Lauren UK, http://www.polo-outletstoreonline.com/
Beats by Dre, http://www.discountbeatsbydresale.com/
Sacs Longchamp Pairs, http://www.saclongchamppairs.com/
Canada Goose Outlet, http://www.thecanadagooseoutlet.com/
Hollister UK Shops, http://www.cheaphollistersale.co.uk/
Michael Kors Outlet Online, http://www.michaelkors.so/
Marc Jacobs Outlet, http://www.marcjacobsonsale.com/
Michael Kors Outlet, http://www.michaelkorsr.com/
North Jackets Clearance, http://www.north-faceoutletonlines.net/
Burberry Outlet Online, http://www.burbagssale2013.com/
North Clearance Outlet Online, http://www.north-clearance.com/
Coach Factory, http://www.coach-factories.com/
Coach Outlet Online, http://www.coach-purseonsale.com/
Cheap Ralph Lauren UK, http://cheapralphlaurenuk.1minutesite.co.uk/
http://marcjacobsoutlet-2014.tripod.com/
http://www.michaelkorsoutletr.com/
Michael Kors Outlet
Discount Michael Kors
http://www.michael-korsfactorystore.com/
Michael Kors Outlet
Michael Kors Factory Store
http://www.michaellkorsfactoryonline.com/
Michael Kors Outlet
Discount micheal kors

__________

POLO says:

2014-08-04 08:00:26

http://www.superbagsmarket.com/ Super Bags Market
http://www.nike-jordanshoes.com/ Nike Jordan Shoes
http://www.thebeatsbydre.net/ Beats By Dre Outlet
http://www.michaelusakors.com/ Michael Kors Outlet Online
http://www.polo-tshirts.com/ Ralph Polo Outlet Online
http://www.northsclearance.com/ North Clearace Outlet
http://www.polo-ralphlaurenonline.com/ Ralph Polo Outlet
http://www.guccishoesuk-2014.net/ Gucci Shoes Outlet
http://www.michaelkorsstates.com/ Michael Kors Outlet
http://www.polooutlet-store.com/ Polo Outlet Store
http://www.ralph-laurenhome.com/ Ralph Lauren UK
http://www.marcjacobsonsale.com/ Marc Jacobs On Sale
http://www.mcmworldwides.com/ MCM Outlet
http://www.salongchamppairs.com/ Sac Longchamp Pairs
http://www.canada-gooser.com/ Canada Goose Outlet
http://www.burberrybags2014.com/ Burberry Outlet
http://www.michaelkors.so/ Michael Kors Outlet
http://www.oakley-sunglassfactory.com/ Oakley Sunglaases Factory
http://www.north-faceoutlet.us.com/ North Face Outlet Online
http://www.moncler-clearance.com/ Moncler Clearance
http://www.woolrich-clearance.com/ Woolrich Clearance
http://www.barbour-jacketsoutlet.com/ Barbour Jackets Outlet Online
http://www.moncler-jacketsoutletonline.com/ Moncler Jackets Outlet Online
http://www.monsterbeatsbydres.net/ Monster Beats Outlet
http://www.louis-vuittonblackfriday.com/ Louis Vuitton Outlet
http://www.lv-guccishoesfactory.com/ Louis Vuitton Factory
http://www.mcmoutlet-jp.com/ MCM 店铺
http://www.cheapdiscountoutlet.com/ Discount Clothes Sale
http://www.michael-korsfactoryonline.com/ Michael Kors Factory Online
http://www.coach-factoryoutlets.net/ Coach Factory Outlet
http://www.coach-blackfriday.net/ Coach Black Friday
http://www.coach-storeoutletonline.com/ Coach Store Outlet Online
http://www.coach-factoryoutletonlines.com/ Coach Factory Outlet Online
http://www.coachcoachoutlet.net/ Coach Outlet
http://www.coach-factory.net/ Coach Factory
http://www.coach-pursesoutlet.net/ Coach Purses Outlet Online
http://www.coach-outletonline.net/ Coach Outlet USA
http://www.mksfactoryoutlet.com/ Michael Kors Factory Outlet
http://www.zxcoachoutlet.com/ Coach Outlet Online USA
http://www.mischristmas.com/ Coach Factory Outlet Online
http://www.misblackfriday.com/ Black Friday
http://www.newoutletonlinemall.com/ Coach Purses Outlet Online
http://www.clickmichaelkors.com/ Michael Kors USA
http://www.cmichaelkorsoutlet.com/ Michael Kors Outlet USA
http://www.ralphlaurenepolo.com/ Ralph Lauren Polo

__________

hollister clothing store (http://www.rayban--sunglass.com/) says:

2014-08-05 07:26:45

http://www.rayban--sunglass.com/ ray ban sunglasses
http://www.customscopyright.com/ coach factory outlet
http://www.hollisterclothing-store.com/ hollister clothing store
http://www.michael--korsoutletonline.com/ michael kors outlet online
http://www.oakley--sunglass.com/ oakley sunglasses cheap
http://www.saclancelfrance.com/ sac lancel
http://www.katespade--outlet.com/ kate spade outlet
http://www.katespadeoutlet-online.com/ kate spade outlet online
http://www.katespade-online.com/ kate spade online
http://www.coachoutlet-onlinestore.com/ coach factory outlet
http://www.abercrombie-andfitch.com/ abercrombie and fitch
http://www.michaelkors-outletsonline.com/ michael kors outlet
http://www.sacportefeuille.com/ sac portefeuille
http://www.oakley-sunglassesoutlet.com/ oakley sunglasses outlet
http://www.michael--korsoutlet.com/ michael kors outlet
http://www.toryburch-outlet-online.com/ tory burch outlet online
http://www.abercrombieandfitchstore.com/ abercrombie & fitch
http://www.fit-flops.co.uk/ fitflops
http://www.coachoutlet-onlinefactory.com/ coach outlet online
http://www.coachfactoryoutlet-bag.com/ coach factory outlet
http://www.tory-burchshoes.com/ tory burch shoes
http://www.coachfactoryoutletclearance.com/ coach factory outlet
http://www.raybansunglasses-cheap.com/ cheap ray ban sunglasses
http://www.valentino.us.com/ valentino
http://www.fitfloponline.co.uk/ fitflop uk
http://www.coach-outletonlinefactory.com/ coach outlet
http://www.coachoutlet-factorystore.com/ coach outlet store online
http://www.air--jordans.com/ air jordan
http://www.timberlandshoesus.com/ timberland shoes
http://www.michaelkors--handbags.com/ michael kors handbags
http://www.coachfactoryoutletnew.com/ coach factory outlet
http://www.coachfactory-onlineoutlet.com/ coach factory outlet
http://www.truereligionbrandjeans.us.com/ true religion outlet
http://www.truereligionjeans.us.com/ true religion jeans
http://www.coach--handbags.com/ coach outlet online
http://www.louisvuitton--outlet.us/ louis vuitton outlet
http://www.timberland--shoes.com/ timberland boots
http://www.isabelmarantshoes.us.com/ isabel marant shoes
http://www.coachoutletonline-factory.com/ coach outlet online
http://www.michael-korsonlineoutlet.com/ michael kors outlet online
http://www.louisvuitton-handbag.us/ louis vuitton handbags
http://www.coach-outletstoreonline.com/ coach outlet store online
http://www.true-religion-jeans.com/ true religion jeans
http://www.coach-outletfactoryonline.com/ coach factory outlet online
http://www.coachfactoryoutlet-official.com/ coach factory outlet
http://www.louis-vuittonoutlets.com/ louis vuitton outlet
http://www.officialcoachonlinefactorysale.com/ official coach factory outlet
http://www.coachoutlet-storefactory.com/ coach outlet online
http://www.michael-korsoutletsonline.com/ michael kors outlet
http://www.tiffanysjewelry-co.com/ tiffany and co jewelry
http://www.celineoutlet-online.com/ celine outlet
http://www.celine-handbags.com/ celine handbags
http://www.toms--shoes.us/ toms shoes
http://www.fitflopsandalssale.com/ fitflops
http://www.fitflopusaonline.com/ fitflop outlet
http://www.prada-handbag.com/ prada handbags
http://www.tiffanyandcoinc.com/ tiffany jewelry
http://www.prada-outlets.com/ prada outlet
http://www.michael-korsoutletinc.com/ michael kors
http://www.marcjacobs.us.com/ marc jacobs
http://www.supra.us.com/ supra shoes
http://www.ralphlaurenpolo-inc.com/ polo ralph lauren
http://www.tiffanyjewelry.us.com/ tiffany jewelry


Add your comment on this article below:

Sorry, there's an error with your form entries. We really appreciate your comment, so please try again.

Form submitting now...

Name:

Website:

Email address (not displayed):

Enter your comment below: