// First we define a variable of type URLRequest and attach our mp3 file.
var soundRequest:URLRequest = new URLRequest("JeffWofford_Trouble.mp3");
// Then we make a new sound instance and naming it sound_
var sound_:Sound = new Sound();
// Also a sound control to control and adjust later on.
var soundControl:SoundChannel = new SoundChannel();
// last variable is a soundtransform instance, this is to control the volume up and down function. (and we set them to 1,1 thats the right and left speakers).
var transform1:SoundTransform = new SoundTransform(1, 1);
// now we attach the sound file to the sound_ instance we made before.
sound_.load(soundRequest);
// here is four eventlisteners, the control all our buttons, and "listen" and calls functions if the buttons are clicked.
playButton_.addEventListener(MouseEvent.CLICK, playSound);
stopButton_.addEventListener(MouseEvent.CLICK, stopSound);
soundUp_btn.addEventListener(MouseEvent.CLICK, soundUp);
soundDown_btn.addEventListener(MouseEvent.CLICK, soundDown);
// This is the function to control the play button, only thing happening is that we attach the soundcontrol to the sound and calls its play method.
function playSound(event:MouseEvent):void
{
soundControl = sound_.play();
}
//the stop function also uses the coundControl to stop the mp3 file from playing.
function stopSound(event:MouseEvent):void
{
soundControl.stop();
}
//Now this is a bit more tricky (not much but more then just stop and play).
//At the top of our code we declared a soundtranform named transform1
//this is a value, and by saying ++ we add one to the original volume number then attach it to the soundcontrol.soundTransform
function soundUp(event:MouseEvent):void
{
transform1.volume += .1;
soundControl.soundTransform = transform1;
}
//this is the same as above, we just use -- to subtract from the volume number.
function soundDown(event:MouseEvent):void
{
transform1.volume -= .1;
soundControl.soundTransform = transform1;
}
ok, thank you. can you show me how to get total times of MP3 player? I try calculated by this code:
var mySound:Sound= new Sound();
var sl:Number;
mySound.addEventListener(Event.COMPLETE,LoadComplete);
//calculate the total number of minutes
function LoadComplete(e:Event):void
{
sl=mySound.length;
var a:Number;
var b:Numer;
a= Math.round((sl/1000)%60);
b= Math.round((sl/1000)/60);
}
//.........
//time when play Mp3 file
var R:Number;
var Q:Number;
R=Math.round((myChannel.position/1000)%60);
Q= ((myChannel.position/1000)/60);
//................. my problem is :Although playing file Mp3 complete but (a) not equal(R) and (b) not equal(Q).
Can anyone help me. Thanks !
Bookmarks