00:00
00:00
Newgrounds Background Image Theme

mike181 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: External Audio Optimization

2,287 Views | 9 Replies
New Topic Respond to this Topic

As: External Audio Optimization 2006-08-02 17:41:34


AS: Main.

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:

http://webzoom.freew..reak64/musicdemo.fla

Response to As: External Audio Optimization 2006-08-02 17:49:23


Awesome work man!

Response to As: External Audio Optimization 2006-08-02 18:31:02


Thanks.

Response to As: External Audio Optimization 2006-08-02 23:17:02


No more comments?

Response to As: External Audio Optimization 2006-08-03 09:11:44


At 8/2/06 11:17 PM, ChtFreak64 wrote: No more comments?

Its a good tutorial. But it takes a long time to load, there is a way to change the quality so there is less loading time. But i cna't remember it, XD.
Anyway, nice tutorial, pretty simple though:0

From my point of view , lol.

BBS Signature

Response to As: External Audio Optimization 2006-08-21 21:44:08


At 8/3/06 09:11 AM, Depredation wrote: Its a good tutorial. But it takes a long time to load, there is a way to change the quality so there is less loading time. But i cna't remember it, XD.
Anyway, nice tutorial, pretty simple though:0

From my point of view , lol.

Sorry, I've been away. Yes, there is a way to change the quality, but it doesn't have to do with Flash. Just put the sound into something like Audacity (sp?) and export it into a lower quality, and it'll load faster.

Response to As: External Audio Optimization 2006-08-21 21:50:37


That's awesome man, I've always wondered how to do that. But on a lot of major websites they have flash music players which starts right when you go to the page. how is that possible?


BBS Signature

Response to As: External Audio Optimization 2006-08-21 22:09:18


They probably have the music inside the Flash. This loads it outside.

Response to As: External Audio Optimization 2007-10-30 18:59:53


The audio portal kinda works like this (save for it using as 3 so it gets the fancy visualizations).
It is quite simple, start playing the sound before it is completely loaded.


Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.