//首先将一个mp3导入了库中,给声音文件设置了music类
//在场景中建立3个按钮buttonplay,Continuebotton,stopbotton
var musicstar:SoundChannel=new SoundChannel();
var StopPlayPosition:Number;//停止播放的位置
var soundLoopPlay:Boolean=true;//是否循环播放
var soundTF:SoundTransform = new SoundTransform();//控制声音的音量
var music_play:music = new music();
soundTF.volume = 0.5;//控制声音的音量现在是原来音量的一半
musicstar.soundTransform = soundTF;
stopbotton.visible=true;
Continuebotton.visible=false;
function playLoop():void {
if (soundLoopPlay) {//循环播放
musicstar = music_play.play();
musicstar.addEventListener(Event.SOUND_COMPLETE, onSoundPlay);
} else {
musicstar = music_play.play();
}
}
function onSoundPlay(evt:Event):void {
musicstar.stop();//停止播放
playLoop();
}
buttonplay.addEventListener(MouseEvent.CLICK,PLAY_1);
function PLAY_1(e:MouseEvent):void {
stopbotton.visible=true;
Continuebotton.visible=false;
musicstar.stop();
playLoop();
}
stopbotton.addEventListener(MouseEvent.CLICK,STOP_1);
function STOP_1(e:MouseEvent):void {
stopbotton.visible=false;
Continuebotton.visible=true;
StopPlayPosition=musicstar.position;
musicstar.stop();
}
Continuebotton.addEventListener(MouseEvent.CLICK,Continue_1);
function Continue_1(e:MouseEvent):void {
stopbotton.visible=true;
Continuebotton.visible=false;
musicstar=music_play.play(StopPlayPosition);
musicstar.addEventListener(Event.SOUND_COMPLETE, onSoundPlay);
}