Search code examples
mxmlactionscript-3

AS3 sound sound specific timeline playing


I want to play a sound's specific time, i tried using position , but not gonna help , help me please help me , i need to play a file from specific time to a specific time , i tried using current position , but how do i make it stop , i dont wanna use stop or close functions

        protected function application1_applicationCompleteHandler(event:FlexEvent):void
        {
            snd.load(ul);
            trace(snd.bytesTotal);
            trace(snd.bytesLoaded);
            trace(snd.isBuffering);
            snd.addEventListener(Event.COMPLETE,nad);
            snd.addEventListener(IOErrorEvent.IO_ERROR,rightu);

        }
        protected function rightu(event:IOErrorEvent):void
        {
            Alert.show("error");
        }
        public function nad(event:Event):void
        {

            trace(snd.bytesTotal);
            trace(snd.bytesLoaded); 
            trace(snd.isBuffering);

            p1.enabled=true;
        }



        protected function p1_clickHandler(event:MouseEvent):void
        {
            if(p1.label=="PLAY")
            {
                sc=snd.play(pause);
                sc.soundTransform=st;
                p1.label="PAUSE";

            }
            else
            {
                pause= sc.position; 
                sc.stop();
                p1.label="PLAY";

            }

        }


        protected function i1_clickHandler(event:MouseEvent):void
        {
            st.volume += .1;
            if(st.volume > 1)
            {
                st.volume = 1;
            } 
            sc.soundTransform=st;
        }


        protected function d1_clickHandler(event:MouseEvent):void
        {
            st.volume -= .1;
            if(st.volume <0)
            {
                st.volume=0;
            }
            sc.soundTransform=st;
        }


        protected function pa1_clickHandler(event:MouseEvent):void
        {

            sc.stop();
            p1.label="PLAY";
            pause=0;

        }


        protected function test_clickHandler(event:MouseEvent):void
        {
            trace(sc.position);
        }

    ]]>
</fx:Script>
<s:HGroup horizontalAlign="center" verticalAlign="middle">


<s:VGroup verticalAlign="middle" horizontalAlign="center">
    <mx:ProgressBar id="q"/>
    <s:Button id="p1" label="PLAY" enabled="false" click="p1_clickHandler(event)" />
    <s:Button id="pa1" label="STOP" click="pa1_clickHandler(event)"/>
    <s:Button id="i1" label="INCREASE" click="i1_clickHandler(event)"/>
    <s:Button id="d1" label="DECREASE" click="d1_clickHandler(event)"/>
    <s:Button id="test" click="test_clickHandler(event)"/>
</s:VGroup>
</s:HGroup>

Solution

  • If you want to play seconds 21-40 of a 60 second sound(which is what I think you are trying to do), then when you play the sound sound.play(21) then try adding an ENTER_FRAME event listener and checking the position of the channel to stop it at 40...

        private var stopPosition:int = 40000;
        private var pause:int = 21000; // set to 21 secs for example in code
    
        protected function p1_clickHandler(event:MouseEvent):void{
            if(p1.label=="PLAY"){
                sc=snd.play(pause);
                sc.soundTransform=st;
                p1.label="PAUSE";
                addEventListener(Event.ENTER_FRAME, checkPosition);
            } else {
                pause= sc.position; 
                stopTheSound();
            }
        }
    
        private function checkPosition(event:Event):void{
             if(sc.position >= stopPosition){
                  stopTheSound();
             }
        }
    
        private function stopTheSound():void{
             sc.stop();
             p1.label="PLAY";
             removeEventListener(Event.ENTER_FRAME, checkPosition);
        }