A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: [CS3] how to know if my sound is playing or finished playing?

  1. #1
    Member
    Join Date
    May 2007
    Posts
    44

    [CS3] how to know if my sound is playing or finished playing?

    I`m loading and playing sound like below.

    Code:
    //SOUND LOAD
    
    var snd:Sound = new Sound();
    snd.load(new URLRequest("sound.mp3"));
    snd.addEventListener(IOErrorEvent.IO_ERROR, onIOError, false, 0, true);
    snd.addEventListener(Event.COMPLETE, onSoundLoadComplete, false, 0, true);
    
    function onIOError(evt:IOErrorEvent):void{
    	trace("sound loading error: ",evt.text);
    }
    
    function onSoundLoadComplete(evt:Event):void{
    	trace("sound loaded");
    }
    
    //SOUND PLAY
    
    snd_btn1.addEventListener(MouseEvent.MOUSE_OVER,sndPlay);
    
    function sndPlay(evt:Event):void{
    	
    	var channel:SoundChannel
    	channel = snd.play(); 
    }
    i would like to have my sound to play only when it`s not already playing.
    how can I achieve this?

    thank you

  2. #2
    Developing For Dunkets mneil's Avatar
    Join Date
    Mar 2007
    Location
    Lincoln City
    Posts
    2,156
    There is an onSoundStart method you could use. I don't know AS3 real well but I assume it looks like the other onSoundComplete functions. When you call the onSoundStart set a boolean to true, then inside of your sndPlay check it. Add this:

    PHP Code:
    snd.addEventListener(Event.STARTonSoundStartfalse0true);

    function 
    onSoundStart(evt:Event){
        
    evt.target.isStarted true;

    and change this:

    PHP Code:
    function sndPlay(evt:Event):void{
        if(!
    evt.target.isStarted){
           var 
    channel:SoundChannel
           channel 
    snd.play(); 
          }

    Again, I'm no as3 guru so it may not be completely right on the addEventListener part so you may check with the help files on that.
    http://code.mneilsworld.com/
    Text Effects | Bubbles | Dynamic Resize
    4 weeks , 20 papers ... thats 2 dollars .....Caassshhh!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center