Tuesday, July 27, 2010

Sound class in Actionscript 3: Part 2

In this tutorial, we will learn how to create a sound buffer, how to find out the size of our sound file and how to pause the song.

Flash streams sound files - plays them even before they have been fully loaded. That's why it is important to set audio buffers.

mybuffer = new SoundLoaderContext(5000)

The code above creates a 5 second buffer. It can be applied to our sound as the second parameter after URLRequest in load() method or when creating the sound:

var myurl:URLRequest = new URLRequest("mysong.mp3");
var mybuffer:SoundLoaderContext = new SoundLoaderContext(5000);
mysound = new Sound (myurl, mybuffer);
mysound.play();

Or:

var myurl:URLRequest = new URLRequest("mysong.mp3");
var mybuffer:SoundLoaderContext = new SoundLoaderContext(5000);
mysound = new Sound ();
mysound.load(myurl, mybuffer)
mysound.play();

To find out the size of our sound file use bytesTotal property. You can also find out how many bytes are loaded using bytesLoaded. Lets create a progress bar, showing how many percents are loaded from the audio file:

stage.addEventListener(Event.ENTER_FRAME, loop);
var mysound = new Sound(new URLRequest("song.mp3"));
mysound.play();

var barwidth = 200
var barheight = 10

function loop (event:Event){
var loaded = mysound.bytesLoaded;
var total = mysound.bytesTotal;

if(total > 0){
graphics.clear();
graphics.beginFill(0xCCCCCC);
graphics.drawRect(20,20, barwidth, barheight);
graphics.endFill();

var percent = loaded / total

graphics.beginFill(0x0099FF);
graphics.drawRect(20, 20, barwidth * percent, barheight);
graphics.endFill();
}
}

How do we pause our song? Because there's no built-in pause method in flash, we'll need to work a little harder here. Here's the plan:

Song plays, user presses pause button and flash saves the current playback position into a variable and stops the sound. When played again, we just tell flash to start playing music from that position we've specified before.

We will need to create a sound channel, because it has position property.

var mysound:Sound = new Sound();
var mychannel:SoundChannel = new SoundChannel();
var lastPos:Number = 0;
mysound.load(new URLRequest("song.mp3"));
mychannel = mysound.play();

pause_btn.addEventListener(MouseEvent.CLICK, btnPause);

function btnPause(e:MouseEvent):void{
lastPos = mychannel.position;
mychannel.stop();
}

play_btn.addEventListener(MouseEvent.CLICK, btnPlay);

function btnPlay(e:MouseEvent):void{
mychannel = mysound.play(lastPos);
}

There we go!

Related:

Sound class in Actionscript 3: Part 1
Sound class in Actionscript 3: Part 3
Sound class in Actionscript 3: Part 4
Sound class in Actionscript 3: Part 5
Sound class in Actionscript 3: Part 6

2 comments:

Anonymous said...

Everything is very open with a precise clarification of the challenges.
It was definitely informative. Your site is useful. Many thanks for
sharing!
Also visit my web site website

Anonymous said...

I am actually grateful to the holder of this site who has shared this impressive paragraph at at this place.
My web site :: payday money tree loans

Post a Comment