3 Replies Latest reply on Aug 6, 2009 10:26 PM by rlinsurf1

    Play song using external text file

    timboyk1234
      Ok so I have this text file text.txt and it has a variable in it named song1 with the url of a song file. In flash, I set up this actionscript:

      onClipEvent (load) {
      loadVariables("text.txt", "_root");
      var my_sound:Sound = new Sound();
      my_sound.loadSound(song1,false);
      }

      the problem is that I get an UNDEFINED message for the variable song1. But, if I put it in for the variable of dynamic text, the text will show up in the exported flash. I suspect it has something to do with where the variable is being exported to. Anyone have any ideas?
      Help is greatly appreciated.

      -Tim
        • 1. Re: Play song using external text file
          kglad Adobe Community Professional & MVP
          you're using song1 before your text.txt file has completed loading and before song1 is defined.

          to remedy, you can use the LoadVars() object and its onLoad handler or use loadVariables, load into a movieclip and use it's onData handler.
          • 2. Re: Play song using external text file
            Level 7
            timboyk1234 wrote:
            > Ok so I have this text file text.txt and it has a variable in it named song1
            > with the url of a song file. In flash, I set up this actionscript:
            >
            > onClipEvent (load) {
            > loadVariables("text.txt", "_root");
            > var my_sound:Sound = new Sound();
            > my_sound.loadSound(song1,false);
            > }
            >
            > the problem is that I get an UNDEFINED message for the variable song1. But, if
            > I put it in for the variable of dynamic text, the text will show up in the
            > exported flash. I suspect it has something to do with where the variable is
            > being exported to. Anyone have any ideas?
            > Help is greatly appreciated.
            >
            > -Tim
            >

            The LoadVariables is pretty annoying because you have to keep checking
            to see if the file loaded by looking at one of the variables it's
            suppose to pass. If you try and access a variable before it has loaded
            or a variable not in the list you will get undefined.

            onClipEvent (load) {
            var my_sound:Sound = new Sound();
            var target_mc:MovieClip = this;
            var tries:Number = 0;
            loadVariables("text.txt", target_mc);
            function checkLoad () {
            if (target_mc.song1 == undefined) {
            if (++target_mc.tries == 30) {
            // text.txt not loading so prevent infinite loop
            trace("song1 string could not be found.");
            clearInterval(target_mc.id);
            }
            } else {
            target_mc.my_sound.loadSound(target_mc.song1,false);
            clearInterval(target_mc.id);
            }
            }
            var id:Number = setInterval(checkLoad, 100);
            }

            A cleaner way is with LoadVars which has an onLoad event handler you can
            use instead of checking every few milliseconds to see if the file loaded.

            onClipEvent (load) {
            var my_sound:Sound = new Sound();
            var my_lv:LoadVars = new LoadVars();
            my_lv["parent"] = this;
            my_lv.onLoad = function(success:Boolean) {
            if (success) {
            my_lv["parent"].my_sound.loadSound(my_lv["song1"],false);
            } else {
            trace("Error loading/parsing LoadVars.");
            }
            };
            my_lv.load("text.txt");
            }

            Cheers,

            James O'Reilly - JOR
            www.jamesor.com
            • 3. Re: Play song using external text file
              rlinsurf1 Level 1

              Hi, James--

               

              Is there a way to do this without the onClipEvent? Say, just on open?