00:00
00:00
Newgrounds Background Image Theme

LewgusWithFriends just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

As: Sound From Url

3,035 Views | 8 Replies
New Topic Respond to this Topic

As: Sound From Url 2006-08-10 13:42:17


This tutorial is about streaming sound to flash externally from a website or your harddrive. Although you would think doing something like this would be very complicated and just not worth the effort, it's actually suprisingly easy.

First of all, why? Why would you want to load music externally into your flash? I'll tell you. First of all, it cuts down the filesize tremendously as you don't need to include all that music in the flash itself, and secondly it can allow the person playing the flash game or watching the animation to chose what they want to hear. With this sort of thing you could make a jukebox, media player or even a whole game based on the user finding different songs and sounds.

Ok, lets get started with the actual actionscript. In order to load sound via the URL, we use the loadSound method, which looks like this:

loadSound(url:String, isStreaming:Boolean)

You must give the computer 2 bits of information in order for loadSound to work. Firstly the URL which must link to an mp3 file either on the internet or on your harddrive, and secondly whether the sound is an event or streaming sound (true for streaming, false for event). Streaming sounds play while they are downloading whilst event sounds are completely loaded before they start playing.

The first thing you must realise with loadSound is that it can only load mp3's. So if you ever find that it's not working for you, it may well be because you're trying to load a wav file. Also, the loadSound method is available for Flash 7 and higher so people with flash MX cannot use it.

To use loadSound, only 2 lines of code need to be written:

var sound:Sound = new Sound();
sound.loadSound(http://www.sounds.com/soun
d1.mp3
, true);

which demonstrates a sound being streamed, and:

var sound:Sound = new Sound();
sound.loadSound(http://www.sounds.com/soun
d1.mp3
, false);

which demonstrates a an event sound.

As you can see, in the first line of code, a variable called sound is created (it doesn't have to be called sound) and that is used to create a sound object. For more details, read AS: Sound by Denvish.

The sound will play as soon as loaded, and so whenever you want to play a sound from a URL, all that is necessary to do is type those two lines (or just the last line once you have already declared the sound variable).

Having the ability to play music from a URL isn't too great if you have to pre specify the url in the flash document. It can be very easy to allow the person viewing the flash to select which mp3 to play by having an input textbox with a var name of "songURL" and then replacing the last line of code with:
sound.loadSound(songURL, true);
It works just the same.

Flash also allows you to find out certain things about the sound file playing. The most usefull of these are the total length of the sound file and the position of the sound file which is currently playing.

sound.duration will give you the length of the sound file in milliseconds and sound.position will give you the time in milliseconds that have played. These can be useful to display
the total time, time remaining and time gone of the sound file as shown.

trace("This sound file is " + Math.round(_root.sound.duration/1000)+" seconds" + " seconds long");

trace("This sound file has been playing for "+ Math.round(_root.sound.position/1000)+" seconds" +" seconds");

trace("There are " + Math.round((_root.sound.duration/1000)-(_r
oot.sound.position/1000)) + "seconds remaining);

I added Math.round() which will output the number as a round number and divided everything by 1000 in order to get the outputted number from milliseconds to seconds.

I hope this tutorial has been informative and that it will help people bring a larger choice of music into their flashes with this simple but effective technique.

Other things that it would be interesting to look up but i won't explain in this tutorial
(I recommend you google these or look them up on the flash help files).
getBytesLoaded - Finds the number of bytes of the sound file loaded.
getBytesTotal - Finds the total numbr of bytes of the sound file.
getPan - Gives a number from -100 (left) and 100 (right).
getVolume - Gives a number between 0 and 100.
id3 - Can give additional information on sound file (author, year created etc).
onSoundComplete - A function which is called when the sound finishes playing
setPan - Enables you to set which speaker to play the sound out of loudest. (-100-100).
setVolume - Enables you to set the volume (0-100).

Response to As: Sound From Url 2006-08-10 13:43:28


Awwww, shucks!

AS: Main

Knew i forgot something!

Response to As: Sound From Url 2006-08-11 04:06:18


super tutorial, i have been searching for that for ages


BBS Signature

Response to As: Sound From Url 2006-08-11 04:18:07


At 8/10/06 01:43 PM, Cybex wrote: Awwww, shucks!

You should have link all of these.
http://newgrounds.co../topic.php?id=393735
- more on loading
http://newgrounds.co../topic.php?id=394648
- Even more on loading
http://newgrounds.co../topic.php?id=395107
- EVEN MORE on loading (Yes it is by me :P)
http://newgrounds.co../topic.php?id=298829
- crossdomains (very important)
http://newgrounds.co../topic.php?id=230047
- As:Sound by denvish (handy link)

But other whies its all good <:]

Response to As: Sound From Url 2006-08-11 07:02:23


Dude, have you been trying to copy and paste other peoples' code?

At 8/10/06 01:42 PM, Cybex wrote: trace("This sound file is " + Math.round(_root.sound.duration/1000)+" seconds" + " seconds long");

What is adding those two strings supposed to achieve? You should have:

trace("String " + Number + " String");

trace("This sound file has been playing for "+ Math.round(_root.sound.position/1000)+" seconds" +" seconds");

Same here. Code thief :P

Nice try, Eric!


BBS Signature

Response to As: Sound From Url 2006-08-11 08:15:16


At 8/11/06 07:02 AM, _Paranoia_ wrote: Dude, have you been trying to copy and paste other peoples' code?

It was because i did it myself without the Math.round and the /1000 and then i realised i'd already done it better in some flash that i made, so i opened flash and copied and pasted the code that i wrote like a year ago, and it seems i copied an extra +"seconds" as well. But i was the original creator of the code that i copied and pasted so thats no crime.


At 8/10/06 01:42 PM, Cybex wrote: trace("This sound file is " + Math.round(_root.sound.duration/1000)+" seconds" + " seconds long");
What is adding those two strings supposed to achieve? You should have:

trace("String " + Number + " String");

Yes i know, i made a careless mistake, but i did not steal those codes!

Response to As: Sound From Url 2006-08-11 09:38:19


At 8/11/06 08:15 AM, Cybex wrote: Yes i know, i made a careless mistake, but i did not steal those codes!

lolk, I'll give you the benefit of the doubt :)


BBS Signature

Response to As: Sound From Url 2006-08-11 11:23:03


At 8/11/06 08:15 AM, Cybex wrote: Yes i know, i made a careless mistake

and that could confuse noobs.. but I dont think this is somthing to be used unless you understand what you are doing!!

Response to As: Sound From Url 2006-08-11 12:08:59


Dude, since when have you been a 1337 actionscriptz0r? Nice tutorial :P I don't like how you do "hey i am" + "leet :D" (having space between each element) but I'm just nitpicking on your personal coding layout :)


Sup, bitches :)

BBS Signature