5 Replies Latest reply on Apr 14, 2010 10:35 AM by firdosh

    Bufferlength and bufferTime for progressive loading of mp4 video file

    firdosh

      Hey All,

                   I created a simple as3 class which checks if the media state is "buffering" then start a timer and check till the buffer has 5 seconds, but in my time class when I am tracing out bufferlength it gets capped sometimes to a certain value. I am trying to write a buffering logic for progressive loading of video files over slow connections so that it would not play and pause , play and pause but would check if there was 5 seconds loaded in the buffer then only pla. Below is the sample code

       

      Thanks,

      Firdosh

      package
      {
           import com.bit101.components.HUISlider;
           import com.bit101.components.PushButton;
           
           import flash.display.Sprite;
           import flash.events.TimerEvent;
           import flash.utils.Timer;
           
           import org.osmf.containers.MediaContainer;
           import org.osmf.elements.VideoElement;
           import org.osmf.events.BufferEvent;
           import org.osmf.events.MediaPlayerStateChangeEvent;
           import org.osmf.media.DefaultMediaFactory;
           import org.osmf.media.MediaElement;
           import org.osmf.media.MediaPlayer;
           import org.osmf.media.URLResource;
           import org.osmf.utils.URL;
           
           [SWF (width = 600 , height = 400 , backgroundColor = 0xffffff)]
           public class Main extends Sprite
           {
                private var mediaPlayer:MediaPlayer;
                
                public function Main()
                {
                     var playBtn:PushButton = new PushButton(this, 0, 0, "Play");
                     playBtn.width = 75;
                     playBtn.height = 22;
                     var slider:HUISlider = new HUISlider(this, 80, 10);               
                     
                     var videoURL:String = "http://av.vimeo.com/70838/991/9976823.mp4?token=1271199816_e317d08b8f2d7ca4d23fedbe266dd580";
                     var urlResource:URLResource = new URLResource(videoURL);
                     var videoElement:VideoElement = new VideoElement(urlResource);               
                     
                     mediaPlayer = new MediaPlayer();
                     mediaPlayer.bufferLength
                     mediaPlayer.addEventListener(BufferEvent.BUFFER_TIME_CHANGE,onBufferTimeChange ); 
                     mediaPlayer.addEventListener(BufferEvent.BUFFERING_CHANGE ,onBufferChange );
                     mediaPlayer.addEventListener(MediaPlayerStateChangeEvent.MEDIA_PLAYER_STATE_CHANGE, onMediaPlayerStateChange);
                     
                     mediaPlayer.media = videoElement;
                     
                     var mediaContainer:MediaContainer = new MediaContainer();
                     mediaContainer.addMediaElement(videoElement);
                     addChild(mediaContainer);
                }
                
                private function onBufferTimeChange(e:BufferEvent):void
                {
                     trace("BufferTimeChange = " , e.buffering);
                }
                
                private function onBufferChange(e:BufferEvent):void
                {
                     trace("BufferChange = " , e.buffering);
                }
                
                private function onMediaPlayerStateChange(e:MediaPlayerStateChangeEvent):void
                {
                     trace("onMediaPlayerStateChange = " , e.state);
                     if(e.state == "buffering")
                     {     
                          var checkBufferLimit:Timer = new Timer(100);
                          checkBufferLimit.addEventListener(TimerEvent.TIMER , onCheckBufferLength);
                          checkBufferLimit.start();
                          mediaPlayer.pause();               
                     }
                          
                     trace(mediaPlayer.bufferLength);
                }
                
                public function onCheckBufferLength(e:TimerEvent):void
                {
                     trace("onCheckBufferLength = " , mediaPlayer.bufferLength);
                     if(mediaPlayer.bufferLength > 2.5)
                     {
                          trace("onCheckBufferLength = " , mediaPlayer.bufferLength);
                          var timer:Timer = e.currentTarget as Timer;
                          timer.stop();
                          timer.removeEventListener(TimerEvent.TIMER,onCheckBufferLength);                    
                          timer = null;
                          mediaPlayer.play();
                          
                     }
                }
                
                
                
           }
      }
      

       

       

       

       

      onCheckBufferLength =  0.001
      onCheckBufferLength =  0.001
      onCheckBufferLength =  0.48

      onCheckBufferLength =  0.48
      onCheckBufferLength =  0.52
      onCheckBufferLength =  0.96

      onCheckBufferLength =  0.96

      onCheckBufferLength =  1.4

      onCheckBufferLength =  1.44

      onCheckBufferLength =  1.44
      onCheckBufferLength =  1.8
      onCheckBufferLength =  1.92

      onCheckBufferLength =  1.92
      onCheckBufferLength =  2.12
      onCheckBufferLength =  2.36
      onCheckBufferLength =  2.52
      onCheckBufferLength =  2.52

       

      And then it keeps tracing out 2.52 all the time

       

       

      Next Instance it keeps tracing out 555

       

      nCheckBufferLength =  0.195
      onCheckBufferLength =  0.195
      onCheckBufferLength =  0.395
      onCheckBufferLength =  0.555
      onCheckBufferLength =  0.555
      onCheckBufferLength =  0.555
      onCheckBufferLength =  0.555