A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [CS3] AS3 - Make Sound Stop/Start!

  1. #1
    Junior Member
    Join Date
    Jun 2008
    Posts
    4

    [CS3] AS3 - Make Sound Stop/Start!

    Hey everybody! The following code works exactly like I want it to by turning on the sound for my flash movie, but once the sound is on I can't figure out how to make the same button turn it off again. Can someone help me?

    Code:
    import flash.media.Sound;
    import flash.media.SoundChannel;
    
    status.text = "Chimes Off";
    
    playButton.addEventListener(MouseEvent.MOUSE_DOWN, playChimes);
    function playChimes(e:MouseEvent):void
    {
    	var chimes:Chimes = new Chimes();
    	var chimesSound:SoundChannel = chimes.play();
    	status.text = "Chimes On";
    	var chimesStatus:Boolean = true;
    	trace ("Playing!");
    }

  2. #2
    Senior Member
    Join Date
    Oct 2007
    Location
    Leeds, UK
    Posts
    118
    You could add a test inside the playChimes function to see what
    status.text is.

    If the chimes are off, turn them on, and if they're on, turn them off.

    If we use status.text for our test, we don't need the chimesStatus Boolean
    (unless you're using that for something else as well).

    Code:
    import flash.media.Sound;
    import flash.media.SoundChannel;
    
    status.text = "Chimes Off";
    
    playButton.addEventListener(MouseEvent.MOUSE_DOWN, playChimes);
    
    // declare chimesSound here, so it's within scope of the else part of our 
    // conditional statement
    var chimesSound:SoundChannel; 
    
    function playChimes(e:MouseEvent):void
    {
      if(status.text == "Chimes Off") // chimes are off, so turn them on
      {
    	var chimes:Chimes = new Chimes();
    	chimesSound = chimes.play();
    	status.text = "Chimes On";
    	var chimesStatus:Boolean = true;  // ? maybe don't need this if we're using status.text in our test
    	trace ("Playing!");
      }
      else // chimes are on, so turn them off
      {
    	chimesSound.stop();   
            status.text = "Chimes Off";
      }
    }

  3. #3
    Junior Member
    Join Date
    Jun 2008
    Posts
    4
    Thankyou so much! It works!

  4. #4
    ©KatSOft katsoft's Avatar
    Join Date
    Aug 2004
    Location
    Romania
    Posts
    138
    Use it too so THANK YOU.

    Catalin.

  5. #5
    Junior Member
    Join Date
    Apr 2010
    Posts
    1
    Great snippet, thank you!

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