Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 12 of 12

Thread: [AS3] Play sound on click of a button

  1. #1

    Play sound on click of a button

    I want to play a sound on click of a button. I can do this in AS 2.0

    //Action Script 2.0
    Code:
     
    snd=new Sound();
    snd.attachSound("try");
    snd.start();
    //Action Script 3.0
    Code:
     
    var snd:Sound = new Sound();
    //what will come here, as .attachSound() is not there in AS 3.0
    snd.play();
    Thanks...

  2. #2
    This works:

    Code:
     
    var snd:Sound = new Sound();
    snd.load(new URLRequest("my.mp3"));
    snd.play();

  3. #3
    Quote Originally Posted by v_gyku View Post
    This works:

    Code:
     
    var snd:Sound = new Sound();
    snd.load(new URLRequest("my.mp3"));
    snd.play();
    Is there a way to attach the sound file to a variable without having to load it externally? I'd rather not have my sound file floating around outside the swf, especially if i'm going to be using it quite often.

  4. #4
    Quote Originally Posted by pfzngn View Post
    Is there a way to attach the sound file to a variable without having to load it externally? I'd rather not have my sound file floating around outside the swf, especially if i'm going to be using it quite often.
    This might be an ugly solution, but aint it possible to place the sound on the first frame of a movieclip and play that mc on each click?

  5. #5
    32
    posts
    Registered User
    sound works just as movieclips, create a sound in the .fla, use the linkage dialog in teh library and set Class to: "MySound1"
    then use it with:

    var snd:MySound1= new MySound1();
    snd.play();

    should work..

  6. #6
    While scouring as3 forums lately there's something I've been noticing people don't understand:
    Not everyone is compiling from Flash.
    So just keep that in mind while you all try to be helpful.
    P.S. sorry I can't help, I'm looking for the answer too. As well as some way to work with opentype fonts using code instead of linkage

  7. #7
    I also want to play sound at the click of a button, but am new to AS3 and don't know how to attach the above code to the actual button. I was wondering if someone could help me combine the following:

    Code:
    var snd:MySound1= new MySound1();
    snd.play();
    
    soundbutton_MC.addEventListener (MouseEvent.CLICK, soundbutton_Handler);
    
    function soundbutton_Handler (e:MouseEvent):void {
    // this is where I need to tell it to play the sound 
    ;
    }
    Thanks for your help!

  8. #8

    Quote Originally Posted by wintercat View Post
    I also want to play sound at the click of a button, but am new to AS3 and don't know how to attach the above code to the actual button. I was wondering if someone could help me combine the following:

    Code:
    var snd:MySound1= new MySound1();
    snd.play();
    
    soundbutton_MC.addEventListener (MouseEvent.CLICK, soundbutton_Handler);
    
    function soundbutton_Handler (e:MouseEvent):void {
    // this is where I need to tell it to play the sound 
    }
    Thanks for your help!
    Just put the snd.play() inside event handler:

    Code:
    var snd:MySound1= new MySound1();
    
    soundbutton_MC.addEventListener (MouseEvent.CLICK, soundbutton_Handler);
    
    function soundbutton_Handler (e:MouseEvent):void {
    snd.play(); 
    }
    Should work if everything else is ok

  9. #9
    thanks so much psych... such a simple thing but it pointed me in the right direction and now I have lots of buttons all happily playing their soundbites

  10. #10
    You are welcome mate, glad i could help

  11. #11
    1
    posts
    Registered User

    JavaScript to control the audio?

    Is it possible to use a Flash audio player like this, but have something like onClick send the actual MP3 file names to the player. I am trying to build a page with a single Flash MP3 player that would then use JavaScript onClick interactions. There would be several photos on each page and the users would just click on the photos to play a different sound for each one. My goal is to only have one Flash audio player that plays MP3 files and I have been trying to use FlashVars, but just can't seem to make the connection yet.

    Quote Originally Posted by psych View Post
    Just put the snd.play() inside event handler:

    Code:
    var snd:MySound1= new MySound1();
    
    soundbutton_MC.addEventListener (MouseEvent.CLICK, soundbutton_Handler);
    
    function soundbutton_Handler (e:MouseEvent):void {
    snd.play(); 
    }
    Should work if everything else is ok

  12. #12
    20
    posts
    Registered User

    Robot1

    Hi,
    I just used this code to get my sound play and added some functions. On button press the sounds starts playing or stops. But once it reached the end I can't get the sound play again, just when I reenter the frame from a different one. Help would be great.

    Code:
    stop();
    
    var sound:CCTVSound = new CCTVSound();
    var soundChannel:SoundChannel;
    var soundChannelPosition:Number = 0;
    var playSound:Boolean = false; //this is what I thought should help to see if the end of the sound is reached to allow to play again, but don't know how to finish the code...
    
    announcement_btn.addEventListener(MouseEvent.CLICK, playAnn);
    
    function playAnn(e:MouseEvent):void
    {
        
        if (soundChannel == null)
        {
            soundChannel = sound.play(soundChannelPosition);
        }
        else
        {
            soundChannelPosition = soundChannel.position;
            soundChannel.stop();
            soundChannel = null;
        }
    }
    Last edited by Maupa; July 15th, 2011 at 09:23 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012