0
votes

I'm new with AS3,I'm building a full flash website where I tried to include background music with play,pause and stop button(the play and pause is the same button). The problem is the music player play automatically when the page load and when I click the HOME button of my website the music plays again and overlap with the music that played first, so what should I do..thanks in advance

stop();

function goHome (e:MouseEvent):void{
gotoAndStop("HOME");
}
home_btn.addEventListener(MouseEvent.CLICK, goHome);

function goAbout (e:MouseEvent):void{
gotoAndStop("COMPANY");
}
company_btn.addEventListener(MouseEvent.CLICK, goAbout);

function goTestimony (e:MouseEvent):void{
gotoAndStop("TESTIMONY");
}
news_btn.addEventListener(MouseEvent.CLICK, goTestimony);

function goProduct (e:MouseEvent):void{
gotoAndStop("PRODUCT");
}
product_btn.addEventListener(MouseEvent.CLICK, goProduct);

function goContact (e:MouseEvent):void{
gotoAndStop("CONTACT");
}
contacts_btn.addEventListener(MouseEvent.CLICK, goContact);

//imports the necessary as events
import flash.events.Event
import flash.events.MouseEvent;

var isPlaying:Boolean = new Boolean();
var pausePosition:Number = new Number();

//Create an instance of the Sound class
var soundClip:Sound = new Sound();
//Create a new SoundChannel Object
var sndChannel:SoundChannel = new SoundChannel();

//Load sound using URLRequest
soundClip.load(new URLRequest("thousandyears.mp3"));

//Create an event listener that wll update once sound has finished loading
soundClip.addEventListener(Event.COMPLETE, onComplete, false, 0, true);

controller.addEventListener(MouseEvent.MOUSE_DOWN, btnPressController, false, 0, true);
stop_btn.addEventListener(MouseEvent.MOUSE_DOWN, btnPressStop, false, 0, true);

function onComplete(evt:Event):void {
    //Play loaded sound
    sndChannel = soundClip.play();
    isPlaying = true;
}

function btnPressController(evt:MouseEvent):void
{
    switch(isPlaying)
    {
        case true:
            controller.gotoAndStop(2);
            pausePosition = sndChannel.position; 
            sndChannel.stop();
            isPlaying = false;
        break;
        case false:
            controller.gotoAndStop(1);
            sndChannel = soundClip.play(pausePosition);
            isPlaying = true;
        break;
    }
}

function btnPressStop(evt:MouseEvent):void
{
    pausePosition = 0;
    sndChannel.stop();
    controller.gotoAndStop(2);
    isPlaying = false;
}
1

1 Answers

0
votes

Is this code in the frame 1? Is frame 1 labelled 'HOME'?

If so I would add a new frame at the beginning of the timeline (new frame 1) and move the code into it. This way, the code would still be in frame 1, the frame 'HOME' would be frame 2, and so on.

You would need to slightly update your code:

replace stop() with gotoAndStop('HOME');