15 Replies Latest reply on May 1, 2009 4:23 PM by webguy5612

    Load sound at startup

    webguy5612

      Hello I'm trying to load an mp3 at startup but its not playing. Can someone help me. Here is a code sample

       

      <?xml version="1.0" encoding="utf-8"?>

      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:net="flash.net.*" xmlns:media="flash.media.*" initialize="initApp()"  layout="vertical" backgroundGradientAlphas="[1.0, 1.0]"  backgroundGradientColors="[#E489CD, #FCFCFC]">

       

      <mx:Script>

           <![CDATA[

           import flash.display.MovieClip;

      import flash.media.Sound;

      import flash.events.Event;

      import flash.net.URLRequest;

      import flash.media.SoundChannel;

                  

                 [Bindable]

                  private var _sound:Sound;  

                 private var _soundChannel:SoundChannel;

                 

                private function initApp():void {

                var _sound:Sound = new Sound();

                var request:URLRequest = new URLRequest ("audio/Itrustyou.mp3");

                _sound.load(request);

                _sound.addEventListener(Event.COMPLETE,onLoad);

               _soundChannel= _sound.play();

                } 

                 private function onLoad(event:Event):void{ 

                enabled = true;

      }

       

                 

                  private function sendInfo():void{

      if(username.text=="" || email.text=="" || msg.text==""){

      mx.controls.Alert.show("Please verify your form!")

      } else {

      contactService.send()

      }

      }

        

         private function thanks(evt:Event):void{

                  if(String(contactService.lastResult)=="ok") currentState = "thanks"

                  else mx.controls.Alert.show("Errors sending email!")

              }

       

       

         ]]>        

      </mx:Script>

       

        • 1. Re: Load sound at startup
          Flex harUI Adobe Employee

          No errors?  Try loading some other file

           

          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

          Blog: http://blogs.adobe.com/aharui

          • 2. Re: Load sound at startup
            Flex harUI Adobe Employee

            Also note that you have a local var _sound hiding private var _sound.

             

            Alex Harui

            Flex SDK Developer

            Adobe Systems Inc.

            Blog: http://blogs.adobe.com/aharui

            • 3. Re: Load sound at startup
              webguy5612 Level 1

              You mean this is the local?  var _sound:Sound = new Sound();

              I played the mp3 in itunes and there's nothing wrong with it

              So I don't need these files?

                private var _sound:Sound;  

              private var _soundChannel:SoundChannel;

              • 4. Re: Load sound at startup
                Michael Borbor Level 4

                I think that Alex pointed you in the right direction, remove the var _sound:Sound = new Sound(); from the initApp or change it to:  _sound:Sound = new Sound();.

                • 5. Re: Load sound at startup
                  webguy5612 Level 1

                  I removed the private var _sound:Sound;  and the mp3 loaded. Thanks. If I made this an actionscript class how would I import this into the application

                  For example

                   

                  package{

                  import flash.display.MovieClip;

                  import flash.media.Sound;

                  import flash.events.Event;

                  import flash.net.URLRequest;

                  import flash.events.IOErrorEvent;

                  import flash.media.SoundChannel;

                   

                  public class Audio extends MovieClip{

                   

                    private var _sound:Sound;      

                    private var _soundChannel:SoundChannel;

                    private var _resumeTime:Number;

                              

                   

                  public function Audio() {

                   

                  _sound = new Sound();  

                  var request:URLRequest = new URLRequest ('Audio/Demo.mp3');

                  _sound.load(request);

                  _sound.addEventListener(Event.COMPLETE,onLoad);

                   

                   

                  _sound.addEventListener(IOErrorEvent.IO_ERROR,onIOError);

                   

                   

                  _sound.play();

                   

                  }

                      private function onLoad(event:Event):void{

                        

                   

                   

                  }

                   

                     private function onOIError(event: IOErrorEvent):void{

                  }

                  }

                  • 6. Re: Load sound at startup
                    Michael Borbor Level 4

                    import Audio;

                     

                    private var MyAudioComponent:Audio=new Audio();

                    • 7. Re: Load sound at startup
                      webguy5612 Level 1

                      I've added play/stop resume to my script. for some reason I'm getting these errors: Call to a possibly undefined method OnPlay/ Call to a possibly undefined method OnStop. Here is my code

                      <mx:Script>

                           <![CDATA[

                           import flash.display.MovieClip;

                      import flash.media.Sound;

                      import flash.events.Event;

                      import flash.net.URLRequest;

                      import flash.media.SoundChannel;

                                  

                                 [Bindable]

                                public var _sound:Sound;

                                public var _soundChannel:SoundChannel;

                                public var _resumeTime:Number;

                               

                                private function initApp():void {

                              

                                var request:URLRequest = new URLRequest ("audio/Itrustyou.mp3");

                                _sound.load(request);

                                _sound.addEventListener(Event.COMPLETE,onLoad);

                             

                                } 

                                 private function onLoad():void{ 

                                enabled = true;

                      }

                       

                                public function onStop():void{

                                  _resumeTime = _soundChannel.position;

                                  _soundChannel.stop();

                       

                      }

                       

                                   public function onPlay():void{ 

                                   _soundChannel = _sound.play(_resumeTime);

                        }           

                                  private function sendInfo():void{

                      if(username.text=="" || email.text=="" || msg.text==""){

                      mx.controls.Alert.show("Please verify your form!")

                      } else {

                      contactService.send()

                      }

                      }

                        

                         private function thanks(evt:Event):void{

                                  if(String(contactService.lastResult)=="ok") currentState = "thanks"

                                  else mx.controls.Alert.show("Errors sending email!")

                              }

                       

                       

                         ]]>        

                      </mx:Script>

                       

                      <mx:LinkButton label="play" id="_play" click= "OnPlay();" icon="@Embed('assets/Play.gif')" textRollOverColor="#cccccc" rollOverColor ="0xe489cd"

                      color ="#FFFFFF"  />

                       

                      <mx:LinkButton label="Stop" id="_stop" click= "OnStop();" icon="@Embed('assets/Stop.gif')" textRollOverColor="#cccccc" rollOverColor ="0xe489cd"

                      color ="#FFFFFF" />

                      • 8. Re: Load sound at startup
                        Michael Borbor Level 4

                        Remember ActionScript in case sensitive your function name is onStop and you're trying to invoke a function OnStop from the click event, they're not the same.

                        • 9. Re: Load sound at startup
                          webguy5612 Level 1

                          I'm sorry, I normally catch stuff like that. I removed the public var _sound and replaced with var _sound:Sound; . Now I'm getting this errors Access of undefined property_sound.

                           

                          <mx:Script>

                               <![CDATA[

                               import flash.display.MovieClip;

                          import flash.media.Sound;

                          import flash.events.Event;

                          import flash.net.URLRequest;

                          import flash.media.SoundChannel;

                                      

                                     [Bindable]

                                  

                                    public var _soundChannel:SoundChannel;

                                    public var _resumeTime:Number;

                                   

                                    private function initApp():void {

                                    var _sound:Sound;

                                    var request:URLRequest = new URLRequest ("audio/Itrustyou.mp3");

                                    _sound.load(request);

                                    _sound.addEventListener(Event.COMPLETE,onLoad);

                                 

                                    } 

                                     private function onLoad():void{ 

                                    enabled = true;

                          }

                           

                                    public function onStop():void{

                                      _resumeTime = _soundChannel.position;

                                      _soundChannel.stop();

                           

                          }

                           

                                       public function onPlay():void{ 

                                       _soundChannel = _sound.play(_resumeTime);

                            }           

                                      private function sendInfo():void{

                          if(username.text=="" || email.text=="" || msg.text==""){

                          mx.controls.Alert.show("Please verify your form!")

                          } else {

                          contactService.send()

                          }

                          }

                            

                             private function thanks(evt:Event):void{

                                      if(String(contactService.lastResult)=="ok") currentState = "thanks"

                                      else mx.controls.Alert.show("Errors sending email!")

                                  }

                           

                           

                             ]]>        

                          </mx:Script>

                          • 10. Re: Load sound at startup
                            Michael Borbor Level 4

                            Don't mess up your own code my friend. Now you define sound withing a function that means that it's scope it's only within that function, so it can only be access from that function, move it the top of the script and make it private if you want.

                            • 11. Re: Load sound at startup
                              webguy5612 Level 1

                              Oh ok, Thanks for being so helpful and quick to respond.I moved the var_sound:Sound; to the top and now its public var_sound:Sound; But now I don't hear sound at startup

                               

                              <mx:Script>

                                   <![CDATA[

                                   import flash.display.MovieClip;

                              import flash.media.Sound;

                              import flash.events.Event;

                              import flash.net.URLRequest;

                              import flash.media.SoundChannel;

                                          

                                         [Bindable]

                                        public var _sound:Sound;

                                        public var _soundChannel:SoundChannel;

                                        public var _resumeTime:Number;

                                       

                                        private function initApp():void {

                                      

                                        var request:URLRequest = new URLRequest ("audio/Itrustyou.mp3");

                                        _sound.load(request);

                                        _sound.addEventListener(Event.COMPLETE,onLoad);

                                     

                                        } 

                                         private function onLoad():void{ 

                                        enabled = true;

                              }

                               

                                        public function onStop():void{

                                          _resumeTime = _soundChannel.position;

                                          _soundChannel.stop();

                               

                              }

                               

                                           public function onPlay():void{ 

                                           _soundChannel = _sound.play(_resumeTime);

                                }           

                                          private function sendInfo():void{

                              if(username.text=="" || email.text=="" || msg.text==""){

                              mx.controls.Alert.show("Please verify your form!")

                              } else {

                              contactService.send()

                              }

                              }

                                

                                 private function thanks(evt:Event):void{

                                          if(String(contactService.lastResult)=="ok") currentState = "thanks"

                                          else mx.controls.Alert.show("Errors sending email!")

                                      }

                               

                               

                                 ]]>        

                              </mx:Script>

                              • 12. Re: Load sound at startup
                                Michael Borbor Level 4

                                Call the default constructor in your var declaration _sound = new Sound();

                                • 13. Re: Load sound at startup
                                  webguy5612 Level 1

                                  You mean like this? I still can't hear the mp3 at load up

                                   

                                  <mx:Script>

                                       <![CDATA[

                                       import flash.display.MovieClip;

                                  import flash.media.Sound;

                                  import flash.events.Event;

                                  import flash.net.URLRequest;

                                  import flash.media.SoundChannel;

                                              

                                             [Bindable]

                                            public var _sound:Sound = new Sound();

                                            public var _soundChannel:SoundChannel;

                                            public var _resumeTime:Number;

                                           

                                            private function initApp():void {

                                          

                                            var request:URLRequest = new URLRequest ("audio/Itrustyou.mp3");

                                            _sound.load(request);

                                            _sound.addEventListener(Event.COMPLETE,onLoad);

                                         

                                            } 

                                             private function onLoad():void{ 

                                            enabled = true;

                                  }

                                   

                                            public function onStop():void{

                                              _resumeTime = _soundChannel.position;

                                              _soundChannel.stop();

                                   

                                  }

                                   

                                               public function onPlay():void{ 

                                               _soundChannel = _sound.play(_resumeTime);

                                    }           

                                  • 14. Re: Load sound at startup
                                    Michael Borbor Level 4

                                    I think you've already had the sound playing so if you're using Flex Builder use the local history to go back to the point where your program was working and then restore that version and apply the new functionality.

                                    • 15. Re: Load sound at startup
                                      webguy5612 Level 1

                                      I was missing this  _soundChannel= _sound.play(); Thanks for helping me

                                       

                                      Final code

                                       

                                       

                                      <![CDATA[

                                           import flash.display.MovieClip;

                                      import flash.media.Sound;

                                      import flash.events.Event;

                                      import flash.net.URLRequest;

                                      import flash.media.SoundChannel;

                                                  

                                                 [Bindable]

                                                public var _sound:Sound = new Sound();

                                                public var _soundChannel:SoundChannel;

                                                public var _resumeTime:Number;

                                               

                                                private function initApp():void {

                                              

                                                var request:URLRequest = new URLRequest ("audio/Itrustyou.mp3");

                                                _sound.load(request);

                                                _sound.addEventListener(Event.COMPLETE,onLoad);

                                               _soundChannel= _sound.play();

                                                } 

                                                 private function onLoad():void{ 

                                                enabled = true;

                                      }

                                       

                                                public function onStop():void{

                                                  _resumeTime = _soundChannel.position;

                                                  _soundChannel.stop();

                                       

                                      }

                                       

                                                   public function onPlay():void{ 

                                                   _soundChannel = _sound.play(_resumeTime);

                                        }