Sometimes Newgrounds will make paragraphs in the middle of the code, screwing it up if you wish to copy and paste. Hopefully, you can sopt if something doesn't look right.
I've kind of made a topic like this, but it didn't focus on this subject. This will show you how to load Mp3 files from the internet to play in your Flash. "Why the hell would that be of any use to me?" you ask? Here are some pro's and con's to the matter:
Pro's
Smaller SWF and FLA file size.
If your Flash takes up too much space to be on Newgrounds because of the music, it will now fit.
No need to compress, you can have perfect quality.
Great for games.
Con's
Longer loading time.
Not good for movies where you have to stream them (won't sync).
Some Actionscript knowledge required.
A host such as MyFileHut is required.
Only Mp3 files will work.
It's actually very easy. Here's an example.
For something like this, make a raw preloader (without the actionscript) which basically contains a line and a bar. The bar will mask the line, showing the progress. Name this bar, creatively, bar.
Leave that alone for right now. On the first frame of the Flash, put this:
sound = new Sound();
sound.loadSound(url, false);
isPlaying = false;
This makes it easy for loading one song in a movie. I'll get to using multiple songs later. In my example, my song was Vicinity of Obscenity, which the url was http://webzoom.freew..20Of%20Obscenity.MP3
. Easy enough, I added this to the top of those three lines:
url="http://webzoom.freewebs.com/cheatfrea
k64/Vicinity%20Of%20Obscenity.MP3"
Back to the preloader. On the movieclip, just put this code:
onClipEvent (enterFrame) {
loading = ((_root.sound.getBytesLoaded()/_root.sound
.getBytesTotal())*100);
bar._xscale = loading;
if (bar._xscale>=100 && _root.isPlaying == false) {
_root.sound.start();
_root.isPlaying = true;
}
}
It won't work unless Local Playback Security is set to Access Network Only, so set that at the bottom of the Flash publish settings.
For a game with different music for different levels, it's a bit more complicated. Let's make an example with two songs. Put this on the first frame:
url="http://www.site.com/song1.mp3"
url2="http://www.site.com/song2.mp3"
sound = new Sound();
sound.loadSound(url, false);
sound2 = new Sound();
sound2.loadSound(url2, false);
The isPlaying isn't really necessary here.
The preloader is pretty user-friendly. Before the level starts, stick one in there with a stop(); action. Use the same script as before, but change:
loading = ((_root.sound.getBytesLoaded()/_root.sound
.getBytesTotal())*100);
Change the sound part to sound2 or whatever you made it as before. Also, replace:
_root.sound.start();
_root.isPlaying = true;
with:
_parent.play();
It depends on what you're making this for, but hopefully you follow. That will cause it to play (going to the actual level) once the sound is loaded. On your level frame, put this:
_root.sound.start();
This will actually load the song once you're at the level. You can replace sound here, too.
To stop the music, the code is simple:
_root.sound.stop();
If something went wrong, here is the FLA for the example above: