在Actionscript 3中一遍又一遍地播放音乐

时间:2011-02-13 17:09:41

标签: actionscript-3

问候语, 我使用带有Actionscript 3的flash开发了一个网站。

我将音乐作为网站的背景,并在网站加载时加载音乐。

但问题是,当我点击按钮在页面(框架)之间移动然后点击button_01时,音乐将再次播放,所以我将在后台播放多次音乐 并且sound_btn将不再起作用,所以即使我点击sound_btn音乐也不会停止。

我正在使用的代码被列出来了。 请告诉我应该修改的内容,以便在从一个页面(框架)移动到另一个页面时,不允许音乐在后台播放多次。

此致

stop();
//number that is redefined when the pause button is hit
var pausePoint:Number = 0.00;
//a true or false value that is used to check whether the sound is currently playing
var isPlaying:Boolean;

//think of the soundchannel as a speaker system and the sound as an mp3 player
var soundChannel:SoundChannel = new SoundChannel();
var sound:Sound = new Sound(new URLRequest("music.mp3"));


//you should set the xstop and xplay values to match the instance names of your stop button and play/pause buttons
//mute_btn.addEventListener(MouseEvent.CLICK, clickStop);
sound_btn.addEventListener(MouseEvent.CLICK, clickPlayPause);

soundChannel = sound.play();
isPlaying = true;
myVideo.stop();

function clickPlayPause(evt:MouseEvent) {
    if (isPlaying) {
        pausePoint = soundChannel.position;
        soundChannel.stop();
        isPlaying = false;
    } else {
        soundChannel = sound.play(pausePoint);
        isPlaying = true;
    }
}


button_01.addEventListener(MouseEvent.CLICK, onClick1);
button_02.addEventListener(MouseEvent.CLICK, onClick2);
button_03.addEventListener(MouseEvent.CLICK, onClick3);
button_04.addEventListener(MouseEvent.CLICK, onClick4);
button_05.addEventListener(MouseEvent.CLICK, onClick5);
button_06.addEventListener(MouseEvent.CLICK, onClick6);


function onClick1(e:MouseEvent):void 
{ 
gotoAndStop(1);



} 

function onClick2(event:MouseEvent):void 
{  
gotoAndStop(2);


} 

function onClick3(event:MouseEvent):void 
{  
gotoAndStop(3);

} 

function onClick4(event:MouseEvent):void 
{  
gotoAndStop(4);

} 

function onClick5(event:MouseEvent):void 
{  
gotoAndStop(5);

} 

function onClick6(event:MouseEvent):void 
{  
gotoAndStop(6);

} 

1 个答案:

答案 0 :(得分:2)

问题是您在单击button_01时将时间轴发送到的帧上初始化了声音代码。每次执行此操作时都会重新初始化。尝试提前一帧初始化您的声音代码,这样一旦页面加载,您就不会再次登陆该帧。

您也可能会发现将页面包装到动画片段中,并使用visible = true / false来更改部分可能比推进时间轴更改部分更好。每次更改节时,该方法都不会导致声音代码重新初始化。像这样的东西:

function onClick1(e:MouseEvent):void 
{ 
    hideAll();
    section_01.visible = true;



} 

function onClick2(event:MouseEvent):void 
{  
    hideAll();
    section_02.visible = true;


} 

function onClick3(event:MouseEvent):void 
{  
    hideAll();
    section_03.visible = true;

} 

function onClick4(event:MouseEvent):void 
{  
    hideAll();
    section_04.visible = true;

} 

function onClick5(event:MouseEvent):void 
{  
    hideAll();
    section_05.visible = true;

} 

function onClick6(event:MouseEvent):void 
{  
    hideAll();
    section_06.visible = true;

}

function hideAll():void
{
    section_01.visible = false;
    section_02.visible = false;
    section_03.visible = false;
    section_04.visible = false;
    section_05.visible = false;
    section_06.visible = false;
}

如果你想要补间转换,可以使用补间类来处理转换,方法是在隐藏函数中补间当前部分,然后在其各自的onCLick函数中补间下一部分。

相关问题