Flash AS3: How to Play External Musics Automatically and Random

Filed Under: Articles, Tip and Tutor by fandy July 28, 2011, 13:32

auto-play-music-in-flash

In two week ago I have some case, I have to create an Multimedia Application Presentation in Flash with ActionScript 3. In my project, I need to play some background music which are have to play automatically and random at the start up from external mp3 files, rather then play the same song at first all the time.

In this post I want to share what I got. The code are simple, to implement only need to create new folder for music files in project folder, naming the files by sequentially. so you can use the code for your projects.

In this case, I have 6 .mp3 files in folder "musics", I named the mp3 files sequels (music1.mp3, … ,music6.mp3).

Here are the Actionscript :

import flash.media.Sound;
import flash.net.URLRequest;
import flash.media.SoundChannel;
import flash.events.Event;

playsong();
//var songs:Array=new Array("music1.mp3","music2.mp3","music3.mp3","music4.mp3");
var sound:Sound;
var soundChannel:SoundChannel;
var randnum:uint;
function playsong()
{
randnum=Math.floor(Math.random()*6);
sound=new Sound();
var songs:String= "music" + randnum + ".mp3";
sound.load(new URLRequest("musics/"+songs));
soundChannel=sound.play();
soundChannel.addEventListener(Event.SOUND_COMPLETE,playrandomsong);
}
function playrandomsong(e:Event)
{
playsong();
}

The notes:

  • in line 13 change the number of 6 with the total number of the music files you have.
  • if you have your music files in different name or if you like to list the files inside the code, remove the comment mark in line 7, remove line 15 and then replace line 16 with this code.
sound.load(new URLRequest("musics/"+musics[randnum]));

Written by fandy

a web and graphic designer whose love art and design and like to share knowledge, resources and inspirations. English not my mother language so don't blame me, I just do the best to sharing, just give me suggestion to fix it. If love my work, you can support me and follow me on Twitter


Share This :


I PROUDLY PRESENT

YOUR COMMENTS! I LOVE 'EM

Sophie says January 9, 2012,22:08

Hi there, great tutorial works great in my project! I was just wondering how you would go about using play and stop buttons to play the music instead of the music playing automatically? any comments on this would be greatly appreciated!

kind regards,
Sophie

John says February 6, 2012,19:56

Thanks for this! I’ve used it to play a set of very short (1 to 3 second long MP3) sounds and it works very well, but it stops playing after a little while. Is there a way to keep it going forever?

fandy says February 13, 2012,16:29

@John, sorry for late reply.
I’m not sure what wrong, but it run properly in my project.
Please download this example file http://dl.dropbox.com/u/24446554/test-load-random-musics.rar and create new folder “musics” and put some mp3 file in this folder (ie. music1.mp3, music2.mp3, etc.)

John says April 5, 2012,14:19

Thanks for the reply. Actually, I was trying to use this to create a piece of random music, using very short MP3s, and the more MP3s I added, the more unpredictable the playback became. Partly due to the fact that there is no listener to evaluate when the sound has downloaded completely, thus truncating the beginnings and ends of the MP3s, and often making the playback stutter. One of the problems that I noticed quickly was that, as with Javascript, randomizing an array often begins with 0 and not 1, so it was getting confused when trying to call up “part0.mp3.”

I am considering importing all the short MP3s into the FLA (altogether they are smaller than 1 MB), exporting them for AS, change their classes to “part0,” “part1,” etc., but since I do not know ActionScript, I don’t know how to code it so that it plays the MP3s from the library once I’ve exported the SWF. If you would be willing to help me on this project, I would be most appreciative. It shouldn’t be too difficult to code, but I’m such a novice.

John says April 6, 2012,07:25

I should have said, the playback is perfectly smooth when the SWF is on the hard disk with the folder containing the MP3s, but when it’s tested on the Internet, calling hundreds of requests a minute, it can be taxing on the server, so I’d like to embed 100 short MP3s and have them play randomly from within the SWF itself.

LEAVE A COMMENT

AND MAKE ME HAPPY

Your email address will not be published. Required fields are marked *

Name*

Email*

Website

Comment

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Our Sponsors

Community News

    • How to delete facebook account

      Setup by Setup guide and tutorial on how to delete facebook account.

    • 10 Useful Photoshop Web Design Layout Tutorials

      In todays post I’ve collected 10 tutorials about creating a website layout in Photoshop. These tutorials cover different types of web design and hopefully will teach you some new tricks and techniques.

    • Designing with Black: Techniques on How to Create this Artwork

      Art covers a diverse range of human activities and using black as an artwork often applies to visual art such as painting, fashion and photography. Here we share some techniques on how to create artworks using black.

    • 30 Stunning Examples of Grayscale Portrait Photography

      A photographers ability to understand and use the light, literally makes or breaks portrait photography. Understanding the subject and his/her pose also do world of good for a photographer. The face and expression of the subject are of utmost importance when taking a portrait. Grayscale photography mostly known as black-and-white photography is mainly composed of shades of gray. Many a times Grayscale are also known as monochromatic photographs because of the presence of one color in the image.

    • 25 Best Colorful Patterns for Your Next Design

      Default Adobe Patterns are not always successful in satisfying every designer need. Sometimes designers need something different, something which is not very common and yet exactly goes with the designer’s work.


    SUBMIT LINK | MORE NEWS

Recent Comments

    • Saya : I will use them and sponsor them in my blog because their cr... more
    • APNA JAHANIAN : will you please recommend any best software to design brochu... more
    • Samuel : High quality large images are always a plus and can add a lo... more
    • Saya : Nice collection thanks... more
    • APNA JAHANIAN : all themes are great and professional look.specially daily p... more
Hookahs F50-531 st0-096 Dedicated Servers Web Hosting

Twitter: ArtfanDesign