A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: attachSound gone, so how can I load random sound?

  1. #1
    Actionscript Developer KigD's Avatar
    Join Date
    Jan 2003
    Location
    georgia
    Posts
    597

    Question attachSound gone, so how can I load random sound?

    Hi,
    I see that attachSound is gone and you have to instantiate a sound object in replacement.

    In other words I can no longer do this:
    Code:
    var S:Sound = new Sound(this);
    S.attachSound("SoundLinkedInLibrary");
    S.start(0,0);
    I have to do:
    Code:
    var S:Sound = new SoundLinkedInLibrary();
    S.play(0,0);
    ;

    However, this is a big problem! I would like to play a random sound from my library! How can I do this without having if statements?

    I used to be able to do this:
    Code:
    var S:Sound = new Sound(this);
    S.attachSound("MusicTrackNumber"+random(5));
    S.start(0,0);
    But I don't see a way of doing this in AS 3? It seems like I have to do:
    Code:
    var rand:Number = Math.floor(Math.randon()*5);
    var S:Sound;
    if (rand == 0)
    S = new MusicTrackNumber0();
    else if (rand == 1)
    S = new MusicTrackNumber1();
    else if...
    
    //etc
    There MUST be a way. Does anyone have any idea or methods they'd like to share?

    Thanks!
    -Danny
    K2xL - My games, tutorials, message boards, and experiments.
    Blog

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    For 4 sounds:

    var u:String = "track"+(Math.round(3*Math.random()))+".mp3";
    var a:URLRequest = new URLRequest(u);
    var b:Sound = new Sound(a);
    b.play ();
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    Actionscript Developer KigD's Avatar
    Join Date
    Jan 2003
    Location
    georgia
    Posts
    597

    Wink

    Well I figured the answer (getDefinitionByName). It wasn't what you posted. What you posted seems like for external mp3s.

    Thanks anyway!
    -Danny
    K2xL - My games, tutorials, message boards, and experiments.
    Blog

  4. #4
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    Yeah, I noticed. Just for completion of this thread.

    var B:Class = getDefinitionByName("track"+(Math.round(3*Math.ran dom()))) as Class;
    var a:Object = new B();
    a.play ();
    - The right of the People to create Flash movies shall not be infringed. -

  5. #5
    Junior Member
    Join Date
    Jul 2007
    Posts
    2
    Hello
    I am using the examples above and I would like to know how can I stop a sound

    for example:

    I have two buttons. Each one plays a differnt sound

    However, when I click on "button 1", the sound from "button 2" keep playing. And if I click on "button 1" again, I will get the same sound playing over.

    Since I have a sound.play() but dont have a sound.stop(), I really dont know how stop the sound

    Thanx

  6. #6
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    To stop sounds you need to use SoundChannel. here is an example:
    PHP Code:
    import flash.events.*;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;
    var 
    channel1:SoundChannel;
    var 
    channel2:SoundChannel;
    button1.addEventListener (MouseEvent.CLICKstartAudio1);
    function 
    startAudio1 (e:MouseEvent)
    {
        var 
    mySound1:Sound = new Sound();
        
    mySound1.load (new URLRequest("track1.mp3"));
        
    channel1 mySound1.play (01);
        try
        {
            
    channel2.stop ();
        }
        catch (
    e:Error)
        {
            
    trace ("Error");
        }
    }
    //
    //
    //
    button2.addEventListener (MouseEvent.CLICKstartAudio2);
    function 
    startAudio2 (e:MouseEvent)
    {
        var 
    mySound2:Sound = new Sound();
        
    mySound2.load (new URLRequest("track2.mp3"));
        
    channel2 mySound2.play (01);
        try
        {
            
    channel1.stop ();
        }
        catch (
    e:Error)
        {
            
    trace ("Error");
        }

    - The right of the People to create Flash movies shall not be infringed. -

  7. #7
    Junior Member
    Join Date
    Jul 2007
    Posts
    2
    thanx a lot!

    worked fine here =))

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