0
votes

I'm really new to AS3. I could use a little help. I'm creating a flash application that I would love for it to start auto playing my (imported) music file (entitled MySound). I want a button for the option to stop the music, and then toggle for the option to start the music again. When the music is playing, I would love for my mp3 to loop.

I've managed to get the toggle and music playing to work. But the music only starts to play when the button is clicked on. I'm having trouble with the other features. Help?

toggle_btn.stop();

var clickOnce:uint=0;
var aSound:Sound = new MySound();
var aChannel:SoundChannel = new SoundChannel();

toggle_btn.addEventListener(MouseEvent.CLICK, togglePlay);

function togglePlay(event:MouseEvent):void {
clickOnce++;

if (clickOnce==1) {
   aChannel=aSound.play();
   toggle_btn.gotoAndStop(2);
}
if (clickOnce==2) {
   SoundMixer.stopAll();
   toggle_btn.gotoAndStop(1);
   clickOnce=0;
}
}

aChannel.addEventListener(Event.SOUND_COMPLETE, soundCompleted);

function soundCompleted(event:Event):void{
   toggle_btn.gotoAndStop(1);
}
1
just put togglePlay(null) at the end of all your code to have it start playing automatically (though be warned, users don't usually like auto playing sounds unless this is a game or something entertainment based) - BadFeelingAboutThis
Luckily it is a game! Anyway, this didn't work. It made what I do have flash at astronomical speeds, and made the music not even start with a button click. - ticklishoctopus
That means you have an error. Make sure you look at your output and compiler errors tab when you get flashing and things not working - BadFeelingAboutThis
TypeError: Error #1009: Cannot access a property or method of a null object reference. - ticklishoctopus

1 Answers

0
votes

Try this:

    var pP:Number = 0;//playback position
    var playing:Boolean = false;
    var aSound:Sound = new MySound();
    var aChannel:SoundChannel;

    toggle_btn.addEventListener(MouseEvent.CLICK, togglePlay);
    togglePlay(null);

    function togglePlay(event:MouseEvent):void {
        playing = !playing;
        if (playing) playS();
        else pauseS();
    }

    function playS() {
        aChannel = aSound.play(pP);
        aChannel.addEventListener(Event.SOUND_COMPLETE, soundCompleted);
        toggle_btn.gotoAndStop(2);
    }

    function pauseS() {
        pP = aChannel.position;
        aChannel.stop();
        aChannel.removeEventListener(Event.SOUND_COMPLETE, soundCompleted);
        toggle_btn.gotoAndStop(1);
    }

    function soundCompleted(event:Event):void {
        pauseS();
        pP = 0;
        playS();
    }