Here is a basic class that will load in an external .mp3 file and output some text when the file has been loaded. You can toggle the audio (play and stop) by simply clicking on the movie. Please note that stopping the audio is different than pausing the music.
Here is the document class LoadSound.as
package
{
import flash.display.Sprite;
import flash.media.*
import flash.net.URLRequest;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.*;
public class LoadSound extends Sprite
{
private var snd:Sound;
private var sndChannel:SoundChannel;
private var output:TextField;
private var ct:uint;
public function LoadSound()
{
showInstructions();
init();
}
private function init():void
{
var snd:Sound = new Sound();
snd.load(new URLRequest("/path/to/mp3/file/"));
snd.addEventListener(IOErrorEvent.IO_ERROR, sndIOError);
snd.addEventListener(ProgressEvent.PROGRESS, sndProgress);
snd.addEventListener(Event.COMPLETE, sndComplete);
}
private function sndIOError(e:IOErrorEvent):void
{
output.text = "An error occured when laoding the sound";
}
private function sndProgress(e:ProgressEvent):void
{
output.text = "Loading: " + Math.round(100 * e.bytesLoaded / e.bytesTotal);
}
private function sndComplete(e:Event):void
{
output.text = "Sound loaded - click the stage to play and stop";
stage.addEventListener(MouseEvent.MOUSE_DOWN, toggleMusic);
var music:Sound = e.target as Sound;
function toggleMusic():void
{
ct++;
if (ct%2)
sndChannel = music.play();
else
sndChannel.stop();
}
}
private function showInstructions():void
{
// Small Black Text Formatting
var insText:TextFormat = new TextFormat();
insText.font = "Arial";
insText.color = 0xffffff;
insText.size = 12;
insText.underline = false;
// output TextField
output = new TextField();
output.defaultTextFormat = insText;
addChild(output);
output.x = 10;
output.y = 35;
output.width = stage.stageWidth;
output.height = 22;
}
}
}
Cool, at last someone that codes just what is necesary… thanks, this helps me a lot… (actually I vasn’t so far from this)
Thanks
If you have ADD, take your ritalyn before you come here. Way too many cool things to distract ya. You’ll forget why you came here, lol Awesome site.
you are a asset to the flash community.
how’d you get this to work? not sure how to get it started in use