开发者

music playing over and over in Actionscript 3

开发者 https://www.devze.com 2023-02-10 11:11 出处:网络
Greeting, I I developed a website using flash with Actionscript 3. I included a music as a background for the website and the music will loaded when the site loaded.

Greeting, I I developed a website using flash with Actionscript 3.

I included a music as a background for the website and the music will loaded when the site loaded.

but the problem that when I click buttons to move between pages(frames) then go and click button_01 the music will play again so I will have music playing more than one time in the background and the sound_btn will not work any more so even I click sound_btn the music will not stop.

the code I'm using is listed down. Please advice me what I should modify to not allow the music play more than one time in the background while moving from one page(frame) to another.

Regards,

stop();
//number that is redefined when the pause button is hit
var pauseP开发者_Go百科oint: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);

} 


The problem is your code for the sound is initialized on the frame that you send the timeline to when clicking button_01. It will reinitialize each time you do that. Try initializing your sound code one frame earlier so that you do not land on that frame ever again once your page loads.

You also might find that wrapping your pages into movieclips, and using visible = true/false to change sections might be a better approach than advancing the timeline to change sections. That method would not result in the sound code reinitializing each time you changed sections. something like this:

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;
}

If you wanted tweened transitions you could use a tweening class to handle the transitions by tweening the current section out in the hide function and then tweening the next section in in its respective onCLick function.

0

精彩评论

暂无评论...
验证码 换一张
取 消