如果有兩個按鈕,button1,button2,當點擊button1,時播放sound1
當點擊button2的時候播放sound2
當點擊,比較快的時候就回發現聲音重複問題
解決辦法
codes
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
var sound1:Sound = new Sound(new URLRequest("1.mp3"));
var sound2:Sound = new Sound(new URLRequest("2.mp3"));
var button1:Button;
var button2:Button;
var player:String = "";
var soundChannel:SoundChannel;
var music:Sound;
button1.addEventListener(MouseEvent.CLICK,clickHandler);
button2.addEventListener(MouseEvent.CLICK,clickHandler);
function clickHandler(e:MouseEvent):void
{
trace(player);
switch(e.currentTarget){
case button1:
music=sound1;
break;
case button2:
music=sound2;
break;
}
if (player != e.currentTarget.name)
{
if (player != null)
{
soundChannel.stop();
}
soundChannel = music.play();
soundChannel.addEventListener(Event.SOUND_COMPLETE,completeHandler);
}
player = e.currentTarget.name;
}
function completeHandler(e:Event):void
{
player = "";
}