• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

SoundChannel event.sound_complete not working? help?

New Here ,
Mar 04, 2010 Mar 04, 2010

Copy link to clipboard

Copied

Hi,

For some reasons, my soundchannel event.sound_complete is not working. Basically, my class is to import sound into it and to allow use to change their volumes and the speedness of the sound.

This is the tutorial for the speed

http://blog.andre-michelle.com/2009/pitch-mp3/

Right now, what I want to do is to auto loop back the song once the song have finished play but I have having troubled doing it. Anyone, can help me?

The code will pass through SoundForward() function

Inside it, it will trigger  _mp3.addEventListener(Event.COMPLETE, complete);

and will activity complete() function.

Inside it, it will trigger  _soundChannel.addEventListener(Event.SOUND_COMPLETE, loopMusic);

but then the function wont trigger once the song have finish it played.

Benson

Thanks

-----------------------------------------------------------------------------------------------

package examples {
   
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.SampleDataEvent;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;
    import flash.utils.ByteArray;
   
    public class SoundForward extends Sprite 
    {
        private const blockSize:int= 3072;

        private var _mp3:Sound;
        private var _sound:Sound;
        private var _soundChannel:SoundChannel;
       
        private var _target:ByteArray;
        private var _position:Number;
        private var _rate:Number;
        private var _stop:Boolean;

       
        private var _pausePoint:Number;
       
        public function SoundForward(url:String, stop:Boolean)
        {
            _target= new ByteArray();
       
            _mp3= new Sound();
            _mp3.addEventListener(Event.COMPLETE, complete);
            _mp3.load(new URLRequest(url));
           
           _stop= stop;
           
            _pausePoint= 0;
            _position= 0.0;
            _rate= 1.0;
           
            _sound= new Sound();
            _sound.addEventListener(SampleDataEvent.SAMPLE_DATA, sampleData);
       
            _soundChannel= new SoundChannel();
           
        }
       
        private function complete(event:Event):void
        {
            _soundChannel= _sound.play();
            _soundChannel.addEventListener(Event.SOUND_COMPLETE, loopMusic);                   
        }
       
       
        private function sampleData(event:SampleDataEvent):void
        {
            //Reuse Instead of Recreation
            _target.position= 0;
           
            //Shortcut
            var data:ByteArray= event.data;
            var scaledBlockSize:Number= blockSize * _rate;
            var positionInt:int= _position;
            var alpha:Number= _position - positionInt;
           
            var positionTargetNum:Number= alpha;
            var positionTargetInt:int= -1;
           
            var need:int= Math.ceil(scaledBlockSize)+2;
            var read:int= _mp3.extract(_target, need, positionInt);
           
            var n:int = read == need? blockSize: read / _rate;
           
            var l0:Number;
            var r0:Number;
            var l1:Number;
            var r1:Number;
           
            for(var i:int= 0; i< n; ++i)
            {
                if(int(positionTargetNum) != positionTargetInt)
                {
                    positionTargetInt= positionTargetNum;
                   
                    //SET Target Read Position
                    _target.position= positionTargetInt << 3;
                   
                    //Read Two Stero samples for linear INTERPOLATION
                    l0= _target.readFloat();
                    r0= _target.readFloat();
                    
                    l1= _target.readFloat();
                    r1= _target.readFloat();
                }
               
                //Write Interpolated amplitudes into stream
                data.writeFloat(l0 + alpha * (l1 - l0) );
                data.writeFloat(r0 + alpha * (r1 - r0) );
               
                //Increase Target position
                positionTargetNum += _rate;       
               
                //INCREASE FRACTION AND CLAMP BETWEEN 0 AND 1
                alpha += _rate;
                while( alpha >= 1.0 ) --alpha;   
            }
           
            //-- FILL REST OF STREAM WITH ZEROs
            if( i < blockSize)
            {
                while( i < blockSize)
                {
                    data.writeFloat( 0.0 );
                    data.writeFloat( 0.0 );
                   
                    ++i;
                }
            }

            //-- INCREASE SOUND POSITION
            _position += scaledBlockSize;
        }
       
        private function loopMusic(e:Event):void
        {
            trace("AB");
        }
   
   
    }
}

TOPICS
ActionScript

Views

3.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 04, 2010 Mar 04, 2010

use the code i suggested.  you should only have one sound instance (_mp3) in your code.

Votes

Translate

Translate
Community Expert ,
Mar 04, 2010 Mar 04, 2010

Copy link to clipboard

Copied

use:


package examples {
   
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.SampleDataEvent;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;
    import flash.utils.ByteArray;
   
    public class SoundForward extends Sprite 
    {
        private const blockSize:int= 3072;

        private var _mp3:Sound;
        private var _sound:Sound;
        private var _soundChannel:SoundChannel;
       
        private var _target:ByteArray;
        private var _position:Number;
        private var _rate:Number;
        private var _stop:Boolean;

       
        private var _pausePoint:Number;
       
        public function SoundForward(url:String, stop:Boolean)
        {
            _target= new ByteArray();
       
            _mp3= new Sound();
            _mp3.addEventListener(Event.COMPLETE, complete);
            _mp3.load(new URLRequest(url));
           
           _stop= stop;
           
            _pausePoint= 0;
            _position= 0.0;
            _rate= 1.0;
           
        }
       
        private function complete(event:Event):void
        {

_mp3.addEventListener(SampleDataEvent.SAMPLE_DATA, sampleData);


            _soundChannel= _mp3.play();
            _soundChannel.addEventListener(Event.SOUND_COMPLETE, loopMusic);                   
        }
       
       
        private function sampleData(event:SampleDataEvent):void
        {
            //Reuse Instead of Recreation
            _target.position= 0;
           
            //Shortcut
            var data:ByteArray= event.data;
            var scaledBlockSize:Number= blockSize * _rate;
            var positionInt:int= _position;
            var alpha:Number= _position - positionInt;
           
            var positionTargetNum:Number= alpha;
            var positionTargetInt:int= -1;
           
            var need:int= Math.ceil(scaledBlockSize)+2;
            var read:int= _mp3.extract(_target, need, positionInt);
           
            var n:int = read == need? blockSize: read / _rate;
           
            var l0:Number;
            var r0:Number;
            var l1:Number;
            var r1:Number;
           
            for(var i:int= 0; i< n; ++i)
            {
                if(int(positionTargetNum) != positionTargetInt)
                {
                    positionTargetInt= positionTargetNum;
                   
                    //SET Target Read Position
                    _target.position= positionTargetInt << 3;
                   
                    //Read Two Stero samples for linear INTERPOLATION
                    l0= _target.readFloat();
                    r0= _target.readFloat();
                    
                    l1= _target.readFloat();
                    r1= _target.readFloat();
                }
               
                //Write Interpolated amplitudes into stream
                data.writeFloat(l0 + alpha * (l1 - l0) );
                data.writeFloat(r0 + alpha * (r1 - r0) );
               
                //Increase Target position
                positionTargetNum += _rate;       
               
                //INCREASE FRACTION AND CLAMP BETWEEN 0 AND 1
                alpha += _rate;
                while( alpha >= 1.0 ) --alpha;   
            }
           
            //-- FILL REST OF STREAM WITH ZEROs
            if( i < blockSize)
            {
                while( i < blockSize)
                {
                    data.writeFloat( 0.0 );
                    data.writeFloat( 0.0 );
                   
                    ++i;
                }
            }

            //-- INCREASE SOUND POSITION
            _position += scaledBlockSize;
        }
       
        private function loopMusic(e:Event):void
        {
            trace("AB");
        }
   
   
    }
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 04, 2010 Mar 04, 2010

Copy link to clipboard

Copied

Hi,

The answer you provide help me to solve my loop issues. However, because of that I cannot triggler my fast forward sound. What should I do?

Unless I have private var _sound:Sound;.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2010 Mar 04, 2010

Copy link to clipboard

Copied

use the code i suggested.  you should only have one sound instance (_mp3) in your code.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 05, 2010 Mar 05, 2010

Copy link to clipboard

Copied

Hi,

Thanks for the recommendation.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 05, 2010 Mar 05, 2010

Copy link to clipboard

Copied

LATEST

you're welcome.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines