//Test.as
package{
import flash.display.*;
import flash.text.*;
import flash.media.*;
import flash.events.*;
import flash.net.URLRequest;
public class Test extends Sprite
{
private var tf:TextField = new TextField;
private var snd:Sound = new Sound();
private var channel:SoundChannel;
private var tfPrevLength:int = -1;
private var tfCurrLength:int = -1;
public function Test()
{
addButton("[STOP]", onClickStop, 0, 0);
addButton("[CLOSE]", onClickClose, 100, 0);
tf.text = "";
tf.autoSize = TextFieldAutoSize.LEFT;
tf.y = 16;
addChild(tf);
snd.addEventListener(ProgressEvent.PROGRESS, onLoadProgress);
snd.addEventListener(Event.COMPLETE, onLoadComplete);
snd.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
snd.load(new URLRequest("test.mp3")); //読み込みが終わるまでにボタンが押せるくらい大きなmp3ファイル。
channel = snd.play();
channel.addEventListener(Event.SOUND_COMPLETE, onSoundComplete);
}
private function addButton(text:String, func:Function, x:int, y:int):void
{
var t:TextField = new TextField();
t.text = text;
t.x = x;
t.y = y;
t.autoSize = TextFieldAutoSize.LEFT;
addChild(t);
t.addEventListener(MouseEvent.CLICK, func);
}
private function onClickStop(e:MouseEvent):void
{
tf.appendText("channel.stopn");
channel.stop();
}
private function onClickClose(e:MouseEvent):void
{
tf.appendText("snd.closen");
snd.close();
}
private function onLoadProgress(e:ProgressEvent):void
{
// Progressイベントは全部表示すると鬱陶しいので、出来るだけまとめる。
if(tfCurrLength == tf.text.length){
tf.text = tf.text.substr(0, tfPrevLength);
}
tfPrevLength = tf.text.length;
tf.appendText("LoadProgress " + e.bytesLoaded + "/" + e.bytesTotal + "n");
tfCurrLength = tf.text.length;
}
private function onLoadComplete(e:Event):void
{
tf.appendText("LoadCompleten");
}
private function onSoundComplete(e:Event):void
{
tf.appendText("SoundCompleten");
}
private function onIOError(e:IOErrorEvent):void
{
tf.appendText("IOError " + e.text);
}
}
}