Unit 14 - MP3 Player
Sound Classes
Sound - This loads sounds and starts a sound paying
SoundChannel - Once a sound plays in Flash, a corresponding SoundChannel object is created for that sound.
SoundMixer - This controls the playback of all sounds that are currently playing in a Flash movie.
Microphone - This captures audio input and controls the properties of the sound stream.
SoundTransform - This controls the volume of a sound and can be applied to Sound, SoundChannel and Microphone objects.
Code Used in Exercise Files
// Define a URlRequest variable and set it to the mp3
var soundRequest:URLRequest=new URLRequest("music.mp3");
// Define a Sound variable and create an instance
var mySound:Sound = new Sound();
// Define a SoundChannel variable and create an instance
var soundControl:SoundChannel = new SoundChannel();
// Define a SoundTransform variable
// this controls the volume up and down function.
// the parameter 1,1 refers to the right and left speaker.
var volumeControl:SoundTransform=new SoundTransform(1,1);
// Sound instance loads .mp3
mySound.load(soundRequest);
mySound.addEventListener(Event.COMPLETE, onComplete);
function onComplete(event:Event):void {
// Add eventlisteners to button instances
play_btn.addEventListener(MouseEvent.CLICK, playSound);
stop_btn.addEventListener(MouseEvent.CLICK, stopSound);
up_btn.addEventListener(MouseEvent.CLICK, soundUp);
down_btn.addEventListener(MouseEvent.CLICK, soundDown);
}
// This function controls the play button
// attach the SoundChannel to the sound and calls its play method.
// The mp3 file has ID3 metadata and the artist and song are displayed
// in the textfield with the instance name trackInfo
function playSound(event:MouseEvent):void {
soundControl.stop();
soundControl=mySound.play();
trackInfo.text="artist: "+mySound.id3.artist+"\n"+"song: "+mySound.id3.songName;
}
//the stop function also uses the soundControl to stop the mp3 file from playing.
function stopSound(event:MouseEvent):void {
soundControl.stop();
}
//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 {
volumeControl.volume += .1;
soundControl.soundTransform=volumeControl;
}
//this is the same as above, we just use -- to subtract from the volume number.
function soundDown(event:MouseEvent):void {
volumeControl.volume -= .1;
soundControl.soundTransform=volumeControl;
}