A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: [RESOLVED] Referencing embedded sounds in an array

  1. #1
    Senior Member
    Join Date
    Apr 2002
    Posts
    113

    resolved [RESOLVED] Referencing embedded sounds in an array

    I want to play an embedded sound referenced from an array.
    In flash 8 I would simply use this code.
    var mySound = new Sound();
    mySound.attachSound(myArray[0][8]);
    mySound.start();
    In CS4 I need to use something like this.

    import flash.media.Sound;
    import flash.media.SoundChannel;

    var mySound:Sound=new Sound();
    var channel:SoundChannel=mySound.play();
    How should I reference the sound file name in the array?
    Thanks.

  2. #2
    Flash/Flex Developer samac1068's Avatar
    Join Date
    Apr 2007
    Location
    Here, no there
    Posts
    1,813
    You can do what you did previous, unless you want to change from AS2.0 to AS3.0. Is that what you're trying to do?
    Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.

  3. #3
    Senior Member
    Join Date
    Apr 2002
    Posts
    113
    Quote Originally Posted by samac1068 View Post
    You can do what you did previous, unless you want to change from AS2.0 to AS3.0. Is that what you're trying to do?

    Yes, I'm trying to force myself into AS3, and finding myself having to relearn a lot.

  4. #4
    Senior Member
    Join Date
    Apr 2002
    Posts
    113
    I couldn't find an easy way to do this so I ended up doing this.


    if (myQuestions[0][8]=="sound1") {
    var mySounda1:sound1 = new sound1();
    var channel1:SoundChannel=mySounda1.play();
    } else if (myQuestions[0][8]=="sound2") {
    var mySounda2:sound2 = new sound2();
    var channel2:SoundChannel=mySounda2.play();
    } else if (myQuestions[0][8]=="sound3") {
    var mySounda3:sound3 = new sound3();
    var channel3:SoundChannel=mySounda3.play();
    } else if (myQuestions[0][8]=="sound4") {
    var mySounda4:sound4 = new sound4();
    var channel4:SoundChannel=mySounda4.play();
    } else if (myQuestions[0][8]=="sound5") {
    var mySounda5:sound5 = new sound5();
    var channel5:SoundChannel=mySounda5.play();
    } else if (myQuestions[0][8]=="sound6") {
    var mySounda6:sound6 = new sound6();
    var channel6:SoundChannel=mySounda6.play();
    } else if (myQuestions[0][8]=="sound7") {
    var mySounda7:sound7 = new sound7();
    var channel7:SoundChannel=mySounda7.play();
    } else if (myQuestions[0][8]=="sound8") {
    var mySounda8:sound8 = new sound8();
    var channel8:SoundChannel=mySounda8.play();
    } else if (myQuestions[0][8]=="sound9") {
    var mySounda9:sound9 = new sound9();
    var channel9:SoundChannel=mySounda9.play();
    } else if (myQuestions[0][8]=="sound10") {
    var mySounda10:sound10 = new sound10();
    var channel10:SoundChannel=mySounda10.play();
    }

    Any advice would be greatly appreciated. I want the sounds in the library since this will be a projector and not a webpage.

  5. #5
    Senior Member
    Join Date
    Jul 2006
    Location
    San Jose, CA
    Posts
    334
    You can use the flash.utils.getDefinitionByName() function to use the elements of an array to instantiate the sound from your fla.

    Ex:

    PHP Code:
    import flash.utils.getDefinitionByName
    import flash
    .media.Sound
    import flash
    .media.SoundChannel

    var sChannel:SoundChannel = new SoundChannel();
    var 
    sounds:Array = new Array("Sound1""Sound2""Sound3");
                
    var 
    soundClass:Class = getDefinitionByName(sounds[1]) as Class;
    var 
    file:Object = new soundClass();            
    sChannel file.play() 
    And in your library you'd have your sounds Linkage set to Sound1, Sound2, etc.
    I (Love | Hate) Flash.
    ----
    Save a version back so others may help you!

  6. #6
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    If you have CS4 create a Vector. You can treat it exactly like an array, since it is an array.

    var mySounds:Vector.<Sounds> = new Vector.<Sounds>();

    You can then directly refer to mySounds without any casting, since the Data type is Sounds.
    - The right of the People to create Flash movies shall not be infringed. -

  7. #7
    Senior Member
    Join Date
    Sep 2000
    Posts
    272

    This may help

    Greetings,

    I too have been moving to AS3 and here is what I came up with as I call many audio files from library

    I establish a new array, tsound in this case.

    var tsound:Array = new Array;

    // Function I call to play sound with linkage id in library. Note tsound[0] in the function

    function nextAudio():void {
    {
    SoundMixer.stopAll();
    var mySound:Sound;
    var librarySound:Class = getDefinitionByName ( tsound[0] ) as Class;
    mySound = new librarySound();
    var myChannel:SoundChannel;
    myChannel = mySound.play();
    myChannel.addEventListener( Event.SOUND_COMPLETE, AudioDoneAction );
    function AudioDoneAction( event:Event ):void {
    // Do something when the audio is done
    trace("done");
    }
    }
    };

    // I assigned the the audio id to the array tsound[0] in a function which is called from elsewhere. Audio plays on a mouse event when the function is called such as Q1() below. //

    function Q1() {
    tsound[0] = "Comp0";
    nextAudio();
    }
    function Q2() {
    tsound[0] = "Comp1";
    nextAudio();
    }

    // Calling a function to play audio from library

    s_btn.addEventListener(MouseEvent.CLICK, rrPlay);

    function rrPlay(event:MouseEvent):void {
    trace("click!!!!!!!!");
    Q1();
    }


    Hope this helps.

    Cheers and Happy New Year!

  8. #8
    Senior Member
    Join Date
    Apr 2002
    Posts
    113
    Thanks for your help Phix. I tried this...

    PHP Code:
    var sChannel:SoundChannel = new SoundChannel();
            var 
    soundClass:Class=getDefinitionByName(myQuestions[0][8]) as Class;
            var 
    file:Object = new soundClass();
            
    sChannel=file.play();
            
    sChannel.addEventListener(Event.SOUND_COMPLETEdoSoundComplete); 
    and I received this error.

    ReferenceError: Error #1065: Variable sound2 is not defined.

    The myQuestions Array is a shuffled nested array with questions, answers and sounds.

  9. #9
    Senior Member
    Join Date
    Apr 2002
    Posts
    113
    Quote Originally Posted by cancerinform View Post
    If you have CS4 create a Vector. You can treat it exactly like an array, since it is an array.

    var mySounds:Vector.<Sounds> = new Vector.<Sounds>();

    You can then directly refer to mySounds without any casting, since the Data type is Sounds.


    I'm sorry cancerinform, but I don't understand.

    Thanks designforce for the idea. This doesn't help me though. I have 50-100 questions in a quiz. I need the sounds to match the questions. In your example I still need to create 50-100 functions to play the sounds.

    I have the questions array set up as nested arrays, each one with a question, an answer and a sound file from the library. The questions are shuffled.

  10. #10
    Senior Member
    Join Date
    Jul 2006
    Location
    San Jose, CA
    Posts
    334
    Quote Originally Posted by Wisconsin View Post
    Thanks for your help Phix. I tried this...

    PHP Code:
    var sChannel:SoundChannel = new SoundChannel();
            var 
    soundClass:Class=getDefinitionByName(myQuestions[0][8]) as Class;
            var 
    file:Object = new soundClass();
            
    sChannel=file.play();
            
    sChannel.addEventListener(Event.SOUND_COMPLETEdoSoundComplete); 
    and I received this error.

    ReferenceError: Error #1065: Variable sound2 is not defined.

    The myQuestions Array is a shuffled nested array with questions, answers and sounds.
    Either the case is wrong (Sound2 vs. sound2) or you don't have a sound2.
    I (Love | Hate) Flash.
    ----
    Save a version back so others may help you!

  11. #11
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    Here is a simple way of doing it. In your library export the Sound for actionscript and give it a class name like Hip1. Then if you collect all sounds in a vector, which later you can use to play them.

    import flash.media.Sound;
    import flash.media.SoundChannel;
    //
    var myVector:Vector.<Sound> = new Vector.<Sound>();
    for (var count:int = 0; count < 3; count++)
    {
    var _Class:Class = getDefinitionByName("Hip" + count) as Class;
    var hp:Sound = new _Class ;
    myVector.push (hp);
    }
    var channel:SoundChannel;
    channel = myVector[1].play(0,1);
    - The right of the People to create Flash movies shall not be infringed. -

  12. #12
    Senior Member
    Join Date
    Apr 2002
    Posts
    113
    Quote Originally Posted by Phix View Post
    Either the case is wrong (Sound2 vs. sound2) or you don't have a sound2.
    This is embarrassing. You were right. Thanks for your help. I imported the sounds from a flash 8 library and 'export in the first frame' wasn't checked

    Thanks cancerinform for your help also.

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