9 Replies Latest reply on Aug 8, 2009 3:05 AM by rlinsurf1

    Load on user click

    rlinsurf1 Level 1

      I'm using the following section of AS:

       

      onClipEvent (load) {
      
      //sets up a sound object, loads the .mp3 file and starts the sound.
                     this.faTrack = new Sound();
                     //use attachSound if the sound is in the library and linked.
                     //use loadSound if the sound is an external .mp3. 
                     //ie comment out one of the next 2 lines.
                     //this.faTrack.attachSound(faFile);
           
                     this.faTrack.loadSound(faFile, true);
                     this.faTrack.start();
      }
      

       

      Then, under the Code: for Frame 1 I have:

       

      // load the FlashAmp data file and target the FlashAmp movieClip on the _root level...
      loadVariables("Introloop.txt", _root.fa);
      

       

      Which the developer says needs to be changed to get the new file in.

       

      In fact, here's from their readme:

       

      3) Loading an external .mp3 file at runtime

       

      This option may seem to be the most attractive and mostly it is, however there

      are some downsides.

       

      To use this option use an equivalent line of code to the following in the

      onClipEvent (load) handler:

       

      this.faTrack.loadSound(faFile, true); 

       

      You do not need to import the sound file into Flash. You just have to make sure

      that the .mp3 file is in the same directory as the .swf file. You should also make

      sure that the .mp3 file is named the same as faFile in the FlashAmp .txt file.

      The benefits of this method are that the .mp3 file only downloads if the user gets

      to the bit in your Flash file where you call it. It also plays as it streams although

      there will be a slight delay as it buffers (FlashAmp will take account of this delay

      as the arrays are based on the position of the sound).

       

       

       

      So I'm lost. How do I need to set this up?

        • 1. Re: Load on user click
          robdillon Most Valuable Participant

          If I'm reading all of this correctly, you just need to set the value of the variable "faFile" to the name of the sound file that you wan to use. Apparently this is in an external text file, "FlashAmp.txt". Open that file in a text editor and look for "faFile".

          1 person found this helpful
          • 2. Re: Load on user click
            rlinsurf1 Level 1

            Hi, Rob--

             

            That looks like it's helped! Thank you.

             

            Ok, but now I'm getting:

             

             

            undefined
            Error opening URL 'file:///jeffrey/Downloads/mp3%5Fplayer%5Ffiles%5Fedit/undefined'
            

             

             

             

            Here's the relevant code as I now have it. This is under Main Actions:

             

            //load the FlashAmp data file and target the FlashAmp movieClip on the _root level...
            loadVariables("Sweethearts_On_Parade_BRGK.txt", _root.fa);
            

             

            Then:

             

             

            function playSound ()
            {
                 if(!this.startLoad){ // sound not loaded
                      this.startLoad = true
                      trace(faFile)
                      //use attachSound if the sound is in the library and linked.
                      /*
                      //attach the sound
                      this.sound.attachSound('myLibrarySound');
                      //automatically call the sound loaded function
                      this.onLoadSound()
                      */
                      //use loadSound if the sound is an external .mp3.
                      this.sound.loadSound (faFile, true);
                      
                 }
                 this.playing = true
                 this.sound.start (this.soundIndex / 1000)
            }
            //pause the current sound
            function pauseSound ()
            {
                 this.playing = false
                 this.soundIndex = this.sound.position
                 this.sound.stop ()
            }
            

             

             

             

            Sweethearts_On_Parade_BRGK.txt and Sweethearts_On_Parade_BRGK.mp3 are both at the root level of the folder containing the .fla. And here's the line in the .txt file itself

             

            &faFile=Sweethearts_On_Parade_BRGK.mp3

             

            So I don't see what I'm doing wrong.

             

            Message was edited by: rlinsurf1

            • 3. Re: Load on user click
              robdillon Most Valuable Participant

              The error is looking at this path.

              jeffrey/Downloads/mp3 player files edit/undefined

              Is this the path to Sweethearts_On_Parade_BRGK.txt" ?

               

              In your playSound() function you have both the loadSound and attachSound methods in use. You only want one of these. You probably want to comment out the attachSound line.

               

              I don't see it here, but you want to be sure that you are waiting for the text file to load before you start to use the contents. Do you have a load complete function for the loadVariables function?

              • 4. Re: Load on user click
                rlinsurf1 Level 1

                Hi, Rob--

                 

                No, I don't. I really think that may be the problem.

                 

                I've actually switched to using loadVars instead. Here's my current code:

                 

                 

                myVars = new LoadVars(); 
                myVars.load("http://charlesnewmanpubl.com/Sweethearts_On_Parade_BRGK.txt");
                
                myVars._path = this   
                myVars.onLoad = function (success) {
                     trace(this._path) 
                     if (success) {
                        trace (myVars.faFile); 
                        for( var prop in this ) {
                            trace (" key " + prop + " = " + this[prop]); 
                        }
                    } else {
                        trace (" Error loading variables "); 
                    }
                }
                

                 

                 

                Then

                 

                 

                //play the current sound
                function playSound ()
                {
                     if(!this.startLoad){ // sound not loaded
                          this.startLoad = true
                          trace("http://charlesnewmanpubl.com/media/" + myVars.faFile)
                
                          //use loadSound if the sound is an external .mp3.
                this.sound.loadSound("http://charlesnewmanpubl.com/media/" + myVars.faFile, true)
                

                 

                 

                 

                Could you tell me how to put in (and where) the load complete function? The .txt does of course load, and you can read the whole thing in the output -- but too late I think. It's fairly large for a text file, and so maybe that's why it's showing the .mp3 (faFile) as being undefined, as although that's the first var in the file, the whole thing hasn't finished loading when the error is thrown.

                • 5. Re: Load on user click
                  rlinsurf1 Level 1

                  Hi, Rob--

                   

                  Ok. I've come along a little further.

                   

                  I got this to work on it's own. This loads, checks, and then plays the file.

                   

                  myVars = new LoadVars(); 
                  
                  myVars.load("http://charlesnewmanpubl.com/Sweethearts_On_Parade_BRGK.txt");
                  
                  myVars.onData = function(src){ 
                       myVars.decode(src)
                       //trace(myVars.faFile);
                       
                       var faFile:String;
                       var my_sound:Sound = new Sound();
                       
                       if (myVars.faFile == undefined) {
                          trace("not yet.");
                      } else {
                          faFile = myVars.faFile;
                          trace(faFile + "!");
                          my_sound.loadSound("http://charlesnewmanpubl.com/media/"+faFile,true);
                          clearInterval(param_interval);
                            
                            my_sound.onLoad = function(success:Boolean) {
                                if (success) {
                                    my_sound.start();
                                    //trace("Sound loaded");
                                } else {
                                    trace("Sound failed");
                                }
                            }
                      }
                  }
                  

                   

                  My output just waits a bit, then displays

                   

                  Sweethearts_On_Parade_BRGK.mp3!

                   

                  But later on in the script, there's this function

                   

                   

                  }function playSound ()
                  {
                       if(!this.startLoad){ // sound not loaded
                            this.startLoad = true
                            trace("http://charlesnewmanpubl.com/media/" + faFile)
                  
                            //use loadSound if the sound is an external .mp3.
                            this.sound.loadSound("http://charlesnewmanpubl.com/media/" + faFile, true);
                  
                       }
                       this.playing = true
                       this.sound.start (this.soundIndex / 1000)
                  }
                  

                   

                  When I run the whole thing, I at first immediately get

                   

                  http://charlesnewmanpubl.com/media/undefined
                  Error opening URL 'http://charlesnewmanpubl.com/media/undefined'
                  

                   

                  And then, after a bit

                   

                  Sweethearts_On_Parade_BRGK.mp3!

                   

                  So it's hitting the playSound function before it's finished loading the data.

                   

                  So it looks like I need to know how to call playSound only after myVars.decode(src). I realize I could just put the play code into the test at the top, but the problem is, the code at the top uses my_sound.xxx and all the rest uses this.sound.xxx. So I just don't know how to fit everything together.

                  • 6. Re: Load on user click
                    robdillon Most Valuable Participant

                    If that first section using loadVars is working then leave it pretty much alone. I would move these two lines:

                    var faFile:String;
                    var my_sound:Sound = new Sound();

                    outside of the onData() function. I would declare those two variables up where you declare myVars. This makes faFile a variable that is always available. It will get an initial value in the onData() function. So when you ask for it later in the playSound function, the variable will have a value.

                     

                    You have a typo in this line:

                    this.sound.loadSoun d("http://charlesnewmanpubl.com/media/" + faFile, true);

                    If you move my_sound as I suggest, then you can use that variable throughout the movie. you won't need this.sound. If you have already loaded any given sound file into my_sound, then you don't need to load it again in order to play it. You should be able to just call my_sound.start(); when you want to play that sound again.

                    1 person found this helpful
                    • 7. Re: Load on user click
                      rlinsurf1 Level 1

                      Hi, Rob--

                       

                      Thank you. I think I'm understanding the var use a little better now.

                       

                      I actually had some help with this last night, so I'm not sure if that's what he did. The file is now playing, but I'm seeing other weirdness.

                       

                      The data in the .txt is there not only to provide the variable of the file name, but a bunch of spectrum data about the file. When that data is used from the project folder, it shows an animated spectrum in the player, but it looks like we're not even getting to any of those functions now when loaded from the server.

                       

                      As well, there's this line:

                       

                      var playing : Boolean = false

                       

                      Which is followed by a bunch of stuff like this:

                       

                       

                      }
                      //sound has finished playing
                      function onSoundComplete(){
                           this.playing = false
                           this.rewind ()
                           if(this.loop){
                                this.playSound ()
                           }
                      }
                      

                       

                      The word playing is highlighted in blue, which means that it isn't a proper var, but somehow this whole thing works anyway. The problem is, I don't know where in this thing it's being used as a var, and where he means it as the parameter, playing.

                       

                      Anyway, I hope you don't mind, but I think I need to post the whole bloody thing here, so here goes:

                       

                       

                       

                      // Create a new LoadVars instance.
                      var fa:LoadVars = new LoadVars();
                      
                      // Give it a reference back to the container clip.
                      fa.parent = this;
                      
                      // Start loading the text file.
                      fa.load("http://charlesnewmanpubl.com/Sweethearts_On_Parade_BRGK.txt");
                      
                      // Gets called after the text file has loaded (or if it fails to load)
                      fa.onLoad = function(success:Boolean)
                         {
                            if (success)
                               {
                                  // Note that the onLoad function is run in the LoadVars context which means you refer to the variables via "this" and not "myVars"
                                  if (this.faFile == undefined)
                                     {
                                        trace("faFile not found.");
                                     }
                                  else
                                     {
                                        trace("faFile has been found: " + this.faFile);
                      
                                        // We use the parent reference we created before to access the sound object instance we created earlier.
                                        this.parent.init();
                                            //this.parent.mp3.loadSound("http://charlesnewmanpubl.com/media/" + this.faFile, true);
                                     }
                               }
                            else
                               {
                                  trace("Could not load text file.");
                               }
                         };
                      
                      
                      var clipInfo;          // Information from Audio clip
                      var clipDuration;     // clip duration
                      var snd;          // Sound object to control volume
                      
                      var graphHolder : MovieClip
                      var loadingTxt : MovieClip
                      var sound : Sound
                      var frameIndex : Number = 1
                      var ampIndex : Number = 1
                      var playing : Boolean = false       //bad var
                      var eqGraph : Boolean = true
                      var soundIndex : Number = 0
                      var graphWidth : Number = 65
                      var graphHeight : Number = 30
                      var graphGap : Number = 1
                      var graphTopMargin : Number = 5
                      var frameLoopInt : Number
                      var loop:Boolean = true
                      var startLoad:Boolean = false
                      
                      //initialisation function
                      function init ()
                      {
                           var ref : Object = this
                           //create an empty movieclip to hold our graph data
                           this.graphHolder.createEmptyMovieClip ('graph', 0)
                           
                           //check for spectrum data to set graph type
                           if (fa.spectArray.length > 0) {
                                eqGraph = true
                           } else { // use amplitude data
                                eqGraph = false
                           }
                           //create our sound object
                           this.sound = new Sound ();
                           this.sound.onSoundComplete = function(){
                                ref.onSoundComplete()
                           }
                           this.playSound ()
                           
                      }
                      //sound has finished playing
                      function onSoundComplete(){
                           this.playing = false
                           this.rewind ()
                           if(this.loop){
                                this.playSound ()
                           }
                      }
                      
                      
                      
                      //play / pause the current sound
                      function playPause ()
                      {
                           if (this.playing)
                           {
                                this.pauseSound ()
                                this.buttonPlay.icon.gotoAndStop('play')
                           }else
                           {
                                this.buttonPlay.icon.gotoAndStop('pause')
                                this.playSound ()
                           }
                      }
                      //rewind the current sound
                      function rewind ()
                      {
                           this.graphHolder.graph.clear ()
                           this.soundIndex = 0
                           this.ampIndex = 0
                           this.sound.stop ()
                           if (this.playing)
                           {
                                this.playSound ()
                           }
                      }
                      //switch between types of graph
                      function switchGraph ()
                      {
                           this.graphHolder.graph.clear ()
                           if (this.eqGraph)
                           {
                                this.ampIndex = 0
                                this.buttonSwitch.icon.gotoAndStop('eq')
                                this.eqGraph = false
                           }else
                           {
                                this.buttonSwitch.icon.gotoAndStop('amp')
                                this.eqGraph = true
                           }
                      }
                      //play the current sound
                      function playSound ()
                      {
                           
                           if(!this.startLoad){ // sound not loaded
                                this.startLoad = true
                      
                                //use loadSound if the sound is an external .mp3.
                                this.sound.loadSound ("http://charlesnewmanpubl.com/" + fa.faFile, true);
                                
                           }
                           this.playing = true
                           this.sound.start (this.soundIndex / 1000)
                      }
                      //pause the current sound
                      function pauseSound ()
                      {
                           this.playing = false
                           this.soundIndex = this.sound.position
                           this.sound.stop ()
                      }
                      //draw eq bar graph
                      function drawEqBars ()
                      {
                           trace("I'm here")        //we're not getting here
                      
                           //works out where the sound is up to in terms of frames.
                           var gFrame : Number = Math.floor ((this.sound.position / 1000) * fa.fps);
                           //work out the width of one eq bar
                           var bWidth : Number = (this.graphWidth - (fa.numBands + 1) * this.graphGap) / fa.numBands
                           //clear the graph canvas from last frame
                           this.graphHolder.graph.clear ()
                           for (var i : Number = 0; i < fa.numBands; i ++)
                           {
                                //work out the height of this bar
                                var bHeight : Number = ((this.graphHeight - this.graphTopMargin) / 100) * fa.spectArray [gFrame][i]
                                //draw the current bar as rectangle
                                this.graphHolder.graph.beginFill (0xFAC096)
                                this.graphHolder.graph.moveTo ((i + 1) * graphGap + i * bWidth, this.graphHeight)
                                this.graphHolder.graph.lineTo ((i + 1) * graphGap + i * bWidth, this.graphHeight - bHeight)
                                this.graphHolder.graph.lineTo ((i + 1) * graphGap + i * bWidth + bWidth, this.graphHeight - bHeight)
                                this.graphHolder.graph.lineTo ((i + 1) * graphGap + i * bWidth + bWidth, this.graphHeight)
                                this.graphHolder.graph.lineTo ((i + 1) * graphGap + i * bWidth, this.graphHeight)
                                this.graphHolder.graph.endFill ()
                           }
                      }
                      //draw amplitude graph
                      function drawAmpBars ()
                      {
                           //works out where the sound is up to in terms of frames (fps is set in the #include file).
                           var gFrame : Number = Math.floor ((this.sound.position / 1000) * fa.fps);
                           //if the graph is at the end then send it back to the start
                           if (this.ampIndex == this.graphWidth)
                           {
                                this.ampIndex = 0
                                this.graphHolder.graph.clear ()
                           }
                           //work out the height of the current line
                           var lineHeight = ((this.graphHeight - (this.graphTopMargin * 2)) / 100) * fa.amplitude [gFrame]
                           //draw our current line as a rectangle
                           this.graphHolder.graph.beginFill (0xFAC096)
                           this.graphHolder.graph.moveTo (this.ampIndex, this.graphHeight / 2 + lineHeight / 2)
                           this.graphHolder.graph.lineTo (this.ampIndex, this.graphHeight / 2 - lineHeight / 2)
                           this.graphHolder.graph.lineTo (this.ampIndex + 1, this.graphHeight / 2 - lineHeight / 2)
                           this.graphHolder.graph.lineTo (this.ampIndex + 1, this.graphHeight / 2 + lineHeight / 2)
                           this.graphHolder.graph.lineTo (this.ampIndex, this.graphHeight / 2 + lineHeight / 2)
                           //increment the line index ready for next frame
                           this.ampIndex ++
                      }
                      //frame looping functions
                      function onEnterFrame ()
                      {
                           //check if the sound is playing
                           if (this.playing)
                           {     
                                // allow playback to start before animating. This gets around the problem
                                // of sound.position reporting > 1 while sound is streaming but not yet playing.
                                if(this.sound.position > 100){
                      
                                     
                                     //check if we are showing the eq graph
                                     if (this.eqGraph)
                                     {
                                          //draw the eq graph  for this frame
                                          this.drawEqBars ()
                                     } else
                                     {
                                          //draw the amplitude graph for this frame
                                          this.drawAmpBars ()
                                     }
                                }
                           }
                      }
                      
                      
                      }
                      //everything's ready. lets go!
                      
                       
                      

                       

                       

                      Any help you can give me with this would be greatly appreciated.

                      • 8. Re: Load on user click
                        rlinsurf1 Level 1

                        Hi, Rob--

                         

                        I just found an online ref. for all AS operators, and playing isn't one of them. Maybe this is just a highlighting issue in CS4?

                         

                        Anyway, if you could just help me troubleshoot why the data isn't being used, I would be very grateful.

                        • 9. Re: Load on user click
                          rlinsurf1 Level 1

                          A little more progress. Again, we first load in a .txt which contain variables for the filename, and a bunch of spectrum data. What's not being used is the spectrum data.

                           

                          I added this to test for what variables were present in the .txt.

                           

                          trace (" variables loaded "); 
                                                 for( var prop in this ) {
                                                trace (" key " + prop + " = " + this[prop]); 
                                                 }
                          

                           

                          Then a trace in the first function that calls for that data.

                           

                          //check for spectrum data to set graph type
                               if (fa.spectArray.length > 0) {
                                         eqGraph = true
                                         trace(fa.spectArray + "hi")
                               } else { // use amplitude data
                                         eqGraph = false
                                         trace("Or not")
                               }          
                          

                           

                          This is a bit of the output:

                           

                          Or not
                          key spectrum = ((0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),

                           

                           

                          So there is no spectArray, but there is spectrum. Is his code right, or is there something else I should be looking for?