1 Reply Latest reply on May 14, 2017 12:05 PM by Thomas_Booth

    Accessing raw sound data tutorial doesnt work for me!!!

    sam luke Level 1

      Hi,

        Just tried importing the Accessing raw sound data (http://livedocs.adobe.com/flex/3/html/help.html?content=Working_with_Sound_14.html) tutorial into flex builder.  It now compiles now, altrhough I needed to include ByteArray import, but when I run it the debug window displays:

       

      Cannot access a property or method of a null object reference.

       

      If I hit continue the music plays though.  I have included the code below:

       

      package {
          import flash.display.Sprite;

       

          public class waveform_example_as extends Sprite
          {
              public function waveform_example_as()
              {           
                  import flash.utils.ByteArray;
                  import flash.display.Graphics;
                  import flash.events.Event;
                  import flash.media.Sound;
                  import flash.media.SoundChannel;
                  import flash.media.SoundMixer;
                  import flash.net.URLRequest;
                 
                  const PLOT_HEIGHT:int = 200;
                  const CHANNEL_LENGTH:int = 256;
                 
                  var snd:Sound = new Sound();
                  var req:URLRequest = new URLRequest("tester.mp3");
                  snd.load(req);
                  
                  var channel:SoundChannel;
                  channel = snd.play();
                  addEventListener(Event.ENTER_FRAME, onEnterFrame);
                  snd.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
                 
                  var bytes:ByteArray = new ByteArray();
                 
                  function onEnterFrame(event:Event):void
                  {
                      SoundMixer.computeSpectrum(bytes, false, 0);
                     
                      var g:Graphics = this.graphics;
                     
                      g.clear();
                      g.lineStyle(0, 0x6600CC);
                      g.beginFill(0x6600CC);
                      g.moveTo(0, PLOT_HEIGHT);
                     
                      var n:Number = 0;
                         
                      // left channel
                      for (var i:int = 0; i < CHANNEL_LENGTH; i++)
                      {
                          n = (bytes.readFloat() * PLOT_HEIGHT);
                          g.lineTo(i * 2, PLOT_HEIGHT - n);
                      }
                      g.lineTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT);
                      g.endFill();
                     
                      // right channel
                      g.lineStyle(0, 0xCC0066);
                      g.beginFill(0xCC0066, 0.5);
                      g.moveTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT);
                     
                      for (i = CHANNEL_LENGTH; i > 0; i--)
                      {
                          n = (bytes.readFloat() * PLOT_HEIGHT);
                          g.lineTo(i * 2, PLOT_HEIGHT - n);
                      }
                      g.lineTo(0, PLOT_HEIGHT);
                      g.endFill();
                  }
                 
                  function onPlaybackComplete(event:Event):void
                  {
                      removeEventListener(Event.ENTER_FRAME, onEnterFrame);
                  }
                 
              }
          }
      }

       

      Thanks,

      Paul.

        • 1. Re: Accessing raw sound data tutorial doesnt work for me!!!
          Thomas_Booth

          //It appears the script was modified.

          //For one, there is an extra bracket. Some lines moved around.

           

          //Anyway, I made these changes and the script then worked fine:

          //changes are commented and marked with my initials. [tb]

           

          //===========================

          package {

              import flash.display.Sprite;         

           

              import flash.utils.ByteArray;                                         //moved the following 7 from below [tb]

              import flash.display.Graphics;

              import flash.events.Event;

              import flash.media.Sound;

              import flash.media.SoundChannel;

              import flash.media.SoundMixer;

              import flash.net.URLRequest;

              import flash.text.TextField

           

           

           

              public class waveform_example_as extends Sprite

              {

                  public function waveform_example_as()

                  {          

          //            import flash.utils.ByteArray;                                  //moved to above [tb]

          //            import flash.display.Graphics;

          //            import flash.events.Event;

          //            import flash.media.Sound;

          //            import flash.media.SoundChannel;

          //            import flash.media.SoundMixer;

          //            import flash.net.URLRequest;

                    

          //           const PLOT_HEIGHT:int = 200;                            // these two lines moved down

          //            const CHANNEL_LENGTH:int = 256;

                    

                      var snd:Sound = new Sound();

                      var req:URLRequest = new URLRequest("tester.mp3");

                      snd.load(req);

                     

                      var channel:SoundChannel;

                      channel = snd.play();

                      addEventListener(Event.ENTER_FRAME, onEnterFrame);

            //          snd.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);             // commented out [tb]

                      channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);         //added line [tb]

                  }

             //        var bytes:ByteArray = new ByteArray();                                                                       // commented out [tb]

                    

            //          function onEnterFrame(event:Event):void

                        private function onEnterFrame(event:Event):void                                                      // made private [tb]

                      {

                          var bytes:ByteArray = new ByteArray();                                                                  // moved from above [tb]

                         

                          const PLOT_HEIGHT:int = 200;                                                  // these two lines moved down from above

                          const CHANNEL_LENGTH:int = 256; // same

                   

                          SoundMixer.computeSpectrum(bytes, false, 0);

                        

                          var g:Graphics = this.graphics;

                        

                          g.clear();

                          g.lineStyle(0, 0x6600CC);

                          g.beginFill(0x6600CC);

                          g.moveTo(0, PLOT_HEIGHT);

                        

                          var n:Number = 0;

                            

                          // left channel

                          for (var i:int = 0; i < CHANNEL_LENGTH; i++)

                          {

                              n = (bytes.readFloat() * PLOT_HEIGHT);

                              g.lineTo(i * 2, PLOT_HEIGHT - n);

                          }

                          g.lineTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT);

                          g.endFill();

                        

                          // right channel

                          g.lineStyle(0, 0xCC0066);

                          g.beginFill(0xCC0066, 0.5);

                          g.moveTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT);

                        

                          for (i = CHANNEL_LENGTH; i > 0; i--)

                          {

                              n = (bytes.readFloat() * PLOT_HEIGHT);

                              g.lineTo(i * 2, PLOT_HEIGHT - n);

                          }

                          g.lineTo(0, PLOT_HEIGHT);

                          g.endFill();

                      }

                    

             //         function onPlaybackComplete(event:Event):void                                         // commented out [tb]

                      private function onPlaybackComplete(event:Event):void                               // made private [tb]

                      {

                          removeEventListener(Event.ENTER_FRAME, onEnterFrame);

                      }

                    

                  }

              }

          //}                                                                                                                // extra bracket commented out [tb]