I need help with bufferlength

Discussion in 'JavaScript' started by coladeu, May 5, 2012.

  1. #1
    Hi! I have a small mp3 player whrited in actionscript and I want to use it for streaming but I have a problem with bufferlength i think. When I press play it plays for a second and then it stops and when I press play again is working fine. This is the .as code of the player
    
    package
    {
        import flash.events.DataEvent;
        import flash.events.Event;
        import flash.events.EventDispatcher;
        import flash.events.TimerEvent;
        import flash.media.Sound;
        import flash.media.SoundChannel;
        import flash.media.SoundMixer;
        import flash.net.URLRequest;
        import flash.system.Capabilities;
        import flash.utils.ByteArray;
        import flash.utils.Timer;
    
        /** GapLess Player
         * Allows to loop music without a gap between.
         * Gapless playback works only in Flash Player > 10.
         * In Flash Player 9 it reverts to standard loop with a gap.
         **/
        public class GLPlayer extends EventDispatcher
        {
            private var src:Sound;
            private var sound:Sound;
            private var bufSize:int;
            private var samplePosition:Number;
            private var sampleCount:Number;
            private var sampleDelta:Number;
            private var _sampleRate:Number;
            private var stdLoop:Boolean;
            private var _channel:SoundChannel;
            private var playing:Boolean;
            private var cue:Number;
            private var len:Number;
            
            public function GLPlayer(r:URLRequest, std:Boolean=false)
            {
                bufSize = 4096;
                samplePosition = 0;
                sampleDelta = 1525;
                _sampleRate = 44.1;
                stdLoop = std;
                playing = false;
                cue = 0;
                len = 0;
                
                var s:String = Capabilities.version.slice(4);
                var ver:int = parseInt(s.split(",")[0], 10);
                if (ver < 10) stdLoop = true;
                
                src = new Sound();
                src.load(r);
                
                sound = new Sound();
                sound.addEventListener("sampleData", onSampleData);
            }
            
            public function playOnce() : void {
                _channel = src.play(0, 1);
                playing = true;
            }
            
            public function playLoop() : void {
                if (playing) return;
                
                if (stdLoop) {
                    _channel = src.play(0, int.MAX_VALUE);
                    playing = true;
                } else if (src.bytesTotal > 0 && src.bytesLoaded == src.bytesTotal) {
                    switchSound();
                } else {
                    _channel = src.play(0, int.MAX_VALUE);
                    src.addEventListener(Event.COMPLETE, switchSound);
                }
            }
            
            public function get channel() : SoundChannel {
                return _channel;
            }
            
            private var switched:Boolean = false;
            private function switchSound(ev:Event=null) : void {
                if (switched) return _loop();
                switched = true;
                
                dispatchEvent(new Event(Event.COMPLETE));
                
                var oldChannel:SoundChannel = _channel;
                if (!_channel) {
                    samplePosition = 0;
                } else {
                    samplePosition = (_channel.position+50)*_sampleRate;
                }
                
                sampleCount = Math.floor(src.length*_sampleRate);
                len = (sampleCount-sampleDelta*2)*1000/44100;
                
                _loop();
                
                if (oldChannel)
                    oldChannel.stop();
            }
            
            public function get length() : Number {
                if (stdLoop || !switched) {
                    return src.length;
                }
                
                return len;
            }
            
            public function get position() : Number {
                if (stdLoop || !switched) {
                    return _channel.position;
                }
                if (!_channel)
                    return 0;
                //return (_channel.position+50)%len;
                return (samplePosition/44.1+50)%len;
            }
            
            public function get canSeek() : Boolean {
                return switched;
            }
            
            public function set position(val:Number) : void {
                if (stdLoop || !switched) {
                    return;
                }
                
                if (val >= len) val = 0;
                samplePosition = val*44.1;
            }
            
            private function _loop() : void {
                if (playing) return;
                playing = true;
                
                _channel = sound.play();
            }
            
            public function pause() : void {
                if (!playing) return;
                playing = false;
                
                cue = _channel.position;
                
                if (stdLoop) {
                    _channel.stop();
                    return;
                }
                
                samplePosition -= 3*bufSize;
                _channel.stop();
            }
            
            private function onSampleData(ev:*) : void {
                //ev : flash.events.SampleDataEvent;
                var i:Number = bufSize;
                while (i>0) {
                    var max:Number = bufSize;
                    if ( (sampleCount - samplePosition-sampleDelta) < bufSize) {
                        max = sampleCount - samplePosition-sampleDelta;
                    }
                    
                    var ba:ByteArray = new ByteArray();
                    var written:Number = src.extract(ba, max-(bufSize-i), samplePosition);
                    ev.data.writeBytes(ba);
                    
                    i -= written;
                    samplePosition += written;
                    if (samplePosition >= sampleCount-sampleDelta) samplePosition = sampleDelta;
                }
            }
            
            public function get isPlaying() : Boolean {
                return playing;
            }
        }
    }
    
    Code (markup):
    And this is the javascript code of the player
    
    var plItems;
    
    /** This function stops other player on website, 
     * so only one is playing at a time.
     **/
    var js_playTune = function js_playTune(pid) {
        plItems.each(function(idx, elem) {
            var gp = $(elem);
            if (gp.attr('id') !== pid) {
                window.document["Fgapl"+idx].halt();
            }
        });
    }
    
    var playerInit = function(idx, elem, playOnce) {
        var item = $(elem);
        var mp3path = item.attr("alt");
        var pid = "gapl" + idx;
        item.prepend('<div class="wh" id="' + pid + '"></div>');
        var so = new SWFObject("ctrlButton.swf", "F"+pid, "40", "30", "9");
        so.addParam("wmode", "transparent");
        so.addVariable("pid", "F"+pid);
        so.addVariable("mp3_path", mp3path);
        if (playOnce) {
            so.addVariable("playOnce", true);
        }
        so.write(pid);
    }
    
    /** This is the main function that embeds GaplessPlayer.
     **/
    $(function() {
      $('.playerItem').each(function(idx, elem) {
            elem = $(elem);
            playerInit(idx, elem, elem.hasClass('once'));
      });
      if ($.browser.msie)
          plItems = $('.playerItem OBJECT');
        else
            plItems = $('.playerItem embed');
        plItems.attr('allowScriptAccess', 'always');
    });
    
    Code (markup):
    The other javascript that is required for the player to work is "swfobject.js" but I don`t think is needed.
    I`m not good at this so I realy need help with buffer implementation or something to make it work fine.
     
    coladeu, May 5, 2012 IP