Service Offerings Why Pariksha Knowledge Centre Newsroom Corporate Contact Us  
  Knowledge Centre|Tips & Tricks
    Case Studies
    Project Examples
    R&D Outsourcing FAQs
    Tips & Tricks
    Newsletters
  Video Player with rewind & forward controls (ActionScript 3.0)  
 


Commonly used analog video players usually have fast forward and rewind buttons, which either play the video with higher speed or play it in reverse, respectively. The major benefit of using these buttons is that user can move across complete video, with a visual perspective of where he is. He has a sense of control over video-playback.

Convenient as these controls might prove to be, they can not be found in any of the Flex’s video related Components. (There are options available to jump to “cue points”, which are more like tagged-points in the video rather than providing a FW/RW functionality). So, can a video really, be played in fast forward and rewind modes using Flex? Sure thing. Let’s have a look.
Assuming a Video object already on the Stage, here’s a small Actionscript 3.0 code –

var ns:NetStream = new NetStream(netConnection);
ns.play("video.mp4");
var speedControlTimer:Timer;
var speedOffset:Number;
speedControlTimer.delay = 100;


function setFFMode():void
{
speedOffset = 2
speedControlTimer.addEventListener(TimerEvent.TIMER,playOnSetSpeed);
speedControlTimer.start();
}


function setRWMode():void
{
speedOffset = -1;
speedControlTimer.addEventListener(TimerEvent.TIMER, playOnSetSpeed);
speedControlTimer.start();
}


function playOnSetSpeed ():void
{
ns.seek(ns.time + speedOffset);
}


function clearModes():void
{
speedControlTimer.stop();
}

How it works
Here, we are using speedControlTimer and speedOffset. speedControlTimer, basically invokes a function on regular time Interval, called playOnSetSpeed().speedOffset is set as -1 when its needed to be played in negative direction ( or rewind) and set to 2 when video has to be Fast-Forwarded.

So now, the scenario is – a function called playOnSetSpeed is called on regular intervals, the interval is specified as 100 ms in code speedControlTimer.delay = 100;

Lets have a closer look at playOnSetSpeed(),which Actually controls the speed of the video.
function playOnSetSpeed ():void
{
ns.seek(ns.time + speedOffset);
}

Well, all it does is call ns.seek(). This call makes video jump to that location. Here, we’re making the video to go to “ns.time + speedOffset”, which means, “Wherever you are right now i.e.’ns.time’, go to a location which is ‘speedOffset’ seconds ahead”. So,when speed offset is negative, video jumps to a location before current time. And if we continuously make video jump to the previous time, it’ll give user a sense of video getting re-wound. Similarly, when speed offset is positive, we are going to a forward position much before the 1x-play would normally reach, so giving a feel of fast forward.

Fine tuning the speedOffset
Experimenting with this speedOffset can result in much better control on the speed of video-playback. To have better control over speed, we need to use some available information:
1 playspeed - the exact speed required. It can be varied as needed, we have used speeds as -16x,-8x,-4x,-2x,-1x for rewinding & 1x, 2x,4x,8x,16x for fast forward modes.

2 timerDelay – to get accurate results we need to take care of intervals at which seek is called. It’s the timer’s ‘delay’ property.

We get actual time to move forward/backward from current time by – “timerDelay * playspeed”. If we want to play at speed 4x, and lets say we call this seek function at duration of 50 ms each. Next location to jump at would be “50 * 4 = 200 ms” from current video location. Essentially, it means, after 50 ms have passed, video should be 200ms ahead, if playing at 4x speed.

Things to keep in mind
Don’t forget to pause the video before starting the timer, doing a seek after pause will show the frame at that location and remain there until next seek comes. If video keeps on playing along with the timer, we’ll need to calculate speedOffset in a totally different way. We’ll need to take care of where video has moved to before calling next seek().

The code works best when used in conjunction with Flash Media Server and mp4 formatted videos. When streaming through FMS, any frame can be rendered without worrying too much about the location of previous key-frame.


Interested in talking to our experts !!  write to products@parikshalabs.com