2 Replies Latest reply on Sep 8, 2010 1:34 AM by Phuong Thanh

    on/off sound by a button in flex

    tracybui

      hello everybody, i want to make a website by flex.in that have sound effect. i want when i load page have sound and if i don't want to listen i can click button.the sound will stop play and else. But i don't know how to make on/off sound by a button click

      i done:

      in actionscript file                Sound_playExample1.as: i write code

      package myComponents {
          import flash.display.Graphics;
          import flash.display.Sprite;
          import flash.events.Event;
          import flash.events.MouseEvent;
          import flash.events.ProgressEvent;
          import flash.media.Sound;
          import flash.media.SoundChannel;
          import flash.net.URLRequest;
         
         
         
          public class Sound_playExample1 extends Sprite {
                      private var snd:Sound = new Sound();
              private var channel:SoundChannel = new SoundChannel();
              public function Sound_playExample1() {           
                  var req:URLRequest = new URLRequest("myComponents/soundall.mp3");
                  snd.load(req);              
                 
                      channel = snd.play(0,6);   
              }       
             
          }
      }
      // ActionScript file

      in my Main.mxml:i write

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                     xmlns:s="library://ns.adobe.com/flex/spark"
                     xmlns:MyComp="myComponents.*"
                     xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

          <fx:Script>
              <![CDATA[
                  protected function button1_clickHandler(event:MouseEvent):void
                  {
                     
                      // TODO Auto-generated method stub
                  }
              ]]>
          </fx:Script>

          <fx:Declarations>
               <MyComp:Sound_playExample1/>
             
              <!-- Place non-visual elements (e.g., services, value objects) here -->
          </fx:Declarations>
          <s:Button x="177" y="71" label="Button" click="button1_clickHandler(event)"/>
         
         
      </s:Application>
      can u help me please what i have to do. if you know you can Additional for this code.thanks

        • 1. Re: on/off sound by a button in flex
          ShardulSingh Level 3

          Hi,

           

          This is complete solution of your issue.Let me know if u find and issue.

           

           

           

           

           

          <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                         xmlns:s="library://ns.adobe.com/flex/spark"
                         xmlns:mx="library://ns.adobe.com/flex/halo"
                        creationComplete="application1_creationCompleteHandler(event)"
                        >

           

              <fx:Script>
                  <![CDATA[
                      import mx.events.FlexEvent;
                     
                      private function onClick():void
                      {
                          createSound();
                          channel = sound.play();
                      }
                     
                      private function offClick():void
                      {
                          if(channel != null)
                          {
                              channel.stop();
                          }
                         
                      }

           

                      private var sound:Sound;
                      private var channel:SoundChannel = new SoundChannel();
                      protected function application1_creationCompleteHandler(event:FlexEvent):void
                      {
                          isPlaying = false;
                          onOff();
                      }
                     
                      private function createSound() : void
                      {
                          var req:URLRequest = new URLRequest("assets/audio1.mp3");
                          if(channel != null)
                          {
                              channel.stop();
                          }
                          if(sound != null)
                          {
                              sound = null;
                          }
                          sound = new Sound();
                          sound.load(req);
                      }

           

                      [Bindable]
                      private var isPlaying : Boolean;
                      protected function onOff():void
                      {
                          if(isPlaying == false)
                          {
                              onClick();
                              isPlaying = true;
                              return;
                          }
                          else
                          {
                              offClick();
                              isPlaying = false;
                          }
                         
                      }

           

                  ]]>
              </fx:Script>

           

              <s:HGroup>
                 
                  <s:Button label="{isPlaying == false? 'ON' : 'OFF'}" click="onOff()"/>
              </s:HGroup>
          </s:Application>

           

           

           

           

           

          with Regards,

           

          Shardul Singh Bartwal

          • 2. Re: on/off sound by a button in flex
            Phuong Thanh Level 1

            wow, great. Thanks so much. i have done.thanks. thanks.!!!!