我只是假设有四个标题/标题按钮和四个标题通道来编写这个示例代码。我有一个标题声音的声道。我可以停止或暂停或播放声音。简单地说,我可以使用titleChannel
.
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.MouseEvent;
var titleSound1:Sound = new TitleSound1();
var titleSound2:Sound = new TitleSound2();
var titleSound3:Sound = new TitleSound3();
var titleSound4:Sound = new TitleSound4();
var mySound:Sound = new heyjude();
var titleChannel:SoundChannel;
var myChannel:SoundChannel;
var lastPosition:Number = 0;
titleBtn1.addEventListener(MouseEvent.CLICK, title1Selected);
titleBtn1.addEventListener(MouseEvent.CLICK, title2Selected);
titleBtn1.addEventListener(MouseEvent.CLICK, title3Selected);
titleBtn1.addEventListener(MouseEvent.CLICK, title4Selected);
play_btn.addEventListener(MouseEvent.CLICK, onClickPlay);
pause_btn.addEventListener(MouseEvent.CLICK, onClickPause);
function title1Selected(e:MouseEvent){
if(titleChannel != null) titleChannel.stop();
titleChannel = titleSound1.play();
}
function title2Selected(e:MouseEvent){
if(titleChannel != null) titleChannel.stop();
titleChannel = titleSound2.play();
}
function title3Selected(e:MouseEvent){
if(titleChannel != null) titleChannel.stop();
titleChannel = titleSound3.play();
}
function title4Selected(e:MouseEvent){
if(titleChannel != null) titleChannel.stop();
titleChannel = titleSound4.play();
}
function onClickPause(e:MouseEvent):void{
lastPosition = myChannel.position;
myChannel.stop();
}
function onClickPlay(e:MouseEvent):void{
myChannel.stop()
myChannel = mySound.play(lastPosition);
}
myChannel
如果您想像其他声音一样暂停和恢复标题声音的播放,请按照您在功能onClickPause
和中所做的相同操作onClickPlay
。
注意:如果您想为某个事件播放声音(例如背景或爆炸期间或其他),并且该事件经常发生,那么请不要犹豫,为该声音创建一个SoundChannel
实例来控制该声音。