12 Replies Latest reply on May 26, 2011 12:00 PM by markerline

    Action script problem for audio player

    dharmes Level 1

      i have made a introduction with a  audio and controller. I am facing one problem. I will like to play as  soon as some one enter the page but it does not.

       

      My action script is pasted below and kindly advise me for any changes that can be done

       

       

      var soundReq:URLRequest = new URLRequest("Sonu Nigam_Alka Yagnik - Suraj Hua M.mp3");
      var sound:Sound = new Sound();
      var soundControl:SoundChannel = new SoundChannel();
      var volumeControl:SoundTransform = new SoundTransform();
      var resumeTime:Number = 0;
      sound.load(soundReq); sound.addEventListener(Event.COMPLETE, onComplete);
      up_btn.addEventListener(MouseEvent.CLICK, increaseVolume);
      down_btn.addEventListener(MouseEvent.CLICK, decreaseVolume);
      function onComplete(event:Event):void
      {
      play_btn.addEventListener(MouseEvent.CLICK, playSound);
      stop_btn.addEventListener(MouseEvent.CLICK, stopSound);
      }
      function playSound(event:MouseEvent):void
      { soundControl = sound.play(resumeTime);
      pause_btn.visible = true;
      pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);
      play_btn.visible = false; play_btn.removeEventListener(MouseEvent.CLICK, playSound);
      }
      function pauseSound(event:MouseEvent):void {
      resumeTime = soundControl.position;
      soundControl.stop();
      play_btn.visible = true;
      play_btn.addEventListener(MouseEvent.CLICK, playSound);
      pause_btn.visible = false;
      pause_btn.removeEventListener(MouseEvent.CLICK, pauseSound);
      }
      function stopSound(event:MouseEvent):void {
      soundControl.stop();
      play_btn.visible = true;
      play_btn.addEventListener(MouseEvent.CLICK, playSound);
      pause_btn.visible = false; pause_btn.removeEventListener(MouseEvent.CLICK, pauseSound);
      }
      function increaseVolume(event:MouseEvent):void {
      volumeControl.volume += .5;
      soundControl.soundTransform = volumeControl;
      }
      function decreaseVolume(event:MouseEvent):void {
      volumeControl.volume -= .5;
      soundControl.soundTransform = volumeControl;
      }
      pause_btn.visible = false;

        • 1. Re: Action script problem for audio player
          kglad Adobe Community Professional & MVP

          :

           


           

           

          var soundReq:URLRequest = new URLRequest("Sonu Nigam_Alka Yagnik - Suraj Hua M.mp3");
          var sound:Sound = new Sound();
          var soundControl:SoundChannel = new SoundChannel();
          var volumeControl:SoundTransform = new SoundTransform();
          var resumeTime:Number = 0;
          sound.load(soundReq); sound.addEventListener(Event.COMPLETE, onComplete);

          playSound(null);

           

          up_btn.addEventListener(MouseEvent.CLICK, increaseVolume);
          down_btn.addEventListener(MouseEvent.CLICK, decreaseVolume);
          function onComplete(event:Event):void
          {
          play_btn.addEventListener(MouseEvent.CLICK, playSound);
          stop_btn.addEventListener(MouseEvent.CLICK, stopSound);
          }
          function playSound(event:MouseEvent):void
          { soundControl = sound.play(resumeTime);
          pause_btn.visible = true;
          pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);
          play_btn.visible = false; play_btn.removeEventListener(MouseEvent.CLICK, playSound);
          }
          function pauseSound(event:MouseEvent):void {
          resumeTime = soundControl.position;
          soundControl.stop();
          play_btn.visible = true;
          play_btn.addEventListener(MouseEvent.CLICK, playSound);
          pause_btn.visible = false;
          pause_btn.removeEventListener(MouseEvent.CLICK, pauseSound);
          }
          function stopSound(event:MouseEvent):void {
          soundControl.stop();
          play_btn.visible = true;
          play_btn.addEventListener(MouseEvent.CLICK, playSound);
          pause_btn.visible = false; pause_btn.removeEventListener(MouseEvent.CLICK, pauseSound);
          }
          function increaseVolume(event:MouseEvent):void {
          volumeControl.volume += .5;
          soundControl.soundTransform = volumeControl;
          }
          function decreaseVolume(event:MouseEvent):void {
          volumeControl.volume -= .5;
          soundControl.soundTransform = volumeControl;
          }
          pause_btn.visible = false;

          • 2. Re: Action script problem for audio player
            markerline Level 4

            You could alternatively add the following code to the first line of your onComplete function:

             

            soundControl=sound.play();

             

            kglad beat me to the post-punch.

            1 person found this helpful
            • 3. Re: Action script problem for audio player
              markerline Level 4

              Also, check the functionality of your volume up and volume down controls.  Right now they appear to be looping once you

              go to the lowest point of volume and click once more it then boosts to a very loud volume in the headphones

              causing potential eardrum damage.

              • 4. Re: Action script problem for audio player
                dharmes Level 1

                Yes, that is another problem i am facing. What can i do to solve that problem?

                • 5. Re: Action script problem for audio player
                  dharmes Level 1

                  just for me to learn, what did you change on my action script?

                  • 6. Re: Action script problem for audio player
                    markerline Level 4

                    Based on what I read in the ActionScript 3.0 Reference : http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/SoundTransf orm.html#volume

                     

                    ... the volume ranges between 0 and 1 not 0 and 100.  So you should make your increments and decrements much smaller, firstly.

                     

                    Secondly, you should create a condition with an 'if' statement that prevents the volume to decrement beyond 0 and prevents the volume to increment beyond 1.  These could be 2 separate if statements or a chained if...else statement.

                    • 7. Re: Action script problem for audio player
                      markerline Level 4

                      kglad added your function playSound and put a null reference inside of it so the function would play regardless of the event.  (Is that right kglad?)

                      1 person found this helpful
                      • 9. Re: Action script problem for audio player
                        dharmes Level 1

                        i am not very good with action script. Relating to my AC, can you kindly show me what and where i need to change my code

                        • 10. Re: Action script problem for audio player
                          markerline Level 4

                          If you are not good with ActionScript how did you generate the above code?  It is not all that basic in nature.  You are declaring variables from various ActionScript 3 methods and applying functions for those variables to be used.

                          • 11. Re: Action script problem for audio player
                            dharmes Level 1

                            i am having big problem with the coding below with the sound. As it we enter the site, the music auto start very loud which is not good at all for the ear drums. How do i adjust that?

                             

                            www.hitenkajal.co.uk

                             

                             

                             

                             

                             

                            var soundReq:URLRequest = new URLRequest("10 Kabhi Alvida Naa Kehna_0.mp3");

                            var sound:Sound = new Sound();

                            var soundControl:SoundChannel = new SoundChannel();

                            var volumeControl:SoundTransform = new SoundTransform();

                            var resumeTime:Number = 0;

                            sound.load(soundReq);

                            sound.addEventListener(Event.COMPLETE, onComplete);

                             

                             

                             

                             

                             

                            up_btn.addEventListener(MouseEvent.CLICK, increaseVolume);

                            down_btn.addEventListener(MouseEvent.CLICK, decreaseVolume);

                            function onComplete(event:Event):void

                            {

                            play_btn.addEventListener(MouseEvent.CLICK, playSound);

                            stop_btn.addEventListener(MouseEvent.CLICK, stopSound);

                             

                            }

                            function playSound(event:MouseEvent):void

                            {

                            soundControl = sound.play(resumeTime);

                            pause_btn.visible = true;

                            pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);

                            play_btn.visible = false;

                            play_btn.removeEventListener(MouseEvent.CLICK, playSound);

                            }

                            function pauseSound(event:MouseEvent):void

                            {

                            resumeTime = soundControl.position;

                            soundControl.stop();

                            play_btn.visible = true;

                            play_btn.addEventListener(MouseEvent.CLICK, playSound);

                            pause_btn.visible = false;

                            pause_btn.removeEventListener(MouseEvent.CLICK, pauseSound);

                            }

                            function stopSound(event:MouseEvent):void

                            {

                            soundControl.stop();

                            play_btn.visible = true;

                            play_btn.addEventListener(MouseEvent.CLICK, playSound);

                            pause_btn.visible = false;

                            pause_btn.removeEventListener(MouseEvent.CLICK, pauseSound);

                            }

                            function increaseVolume(event:MouseEvent):void

                            {

                            volumeControl.volume = 1;

                            soundControl.soundTransform = volumeControl;

                            }

                            function decreaseVolume(event:MouseEvent):void

                            {

                            volumeControl.volume = 0.1;

                            soundControl.soundTransform = volumeControl;

                            }

                            pause_btn.visible = false;

                            playSound(null);

                             

                            • 12. Re: Action script problem for audio player
                              markerline Level 4

                              In the last line of your code you are calling playSound(null) which references your playSound function near the top of your code.

                               

                              What you must do is go inside the playSound function and set the volume as a predefined value similar to the way you have done with the volume controls.

                              0 is silent and 1 is the loudest value.