You will recieve your password to this address. Address is not made public.

Your preferred username that is used when logging in.

Starting play at a particular location with pseudo-streaming? Created Jan 21, 2009

This thread is solved

Views: 2469     Replies: 9     Last reply 2 weeks and 2 days ago  
You must login first before you can use this feature

ezran

Posts: 14

Registered:
Jan 21, 2009

Starting play at a particular location with pseudo-streaming?

Posted: Jan 21, 2009

I've been trying to figure out a simple way to use pseudo-streaming, and have javascript tell the video to start playing at a certain offset (in seconds). Unfortunately, the Player.seek() call only seems to work when the player is paused or playing...

So for the moment, I've come up with this horrendously ugly hack:


    function seekVideo(seconds) {
      var p = $f();
      if (p.isLoaded()) {
        p.seek(seconds);
        if (!p.isPlaying()) p.play();
      } else {
        var c = p.getClip(0);
        var initialLoad = true;
        var seekInterval = null;
        if (c) c.update({ autoPlay: false, autoBuffering: true });

        p.onStart(function () {
          if (initialLoad) { 
            initialLoad = false; 
            p.pause();
            seekInterval = window.setInterval(function() { 
              if (p.isPaused()) {
                window.clearInterval(seekInterval); 
                p.seek(seconds); 
                p.play(); 
              }
            }, 10)
          }
        });
        p.load();
      }
    }

Basically if the video is not yet loaded, I have to tell it to load, catch when the metadata is loaded with onStart(), and then tell it to pause so I can seek. However, then I have to poll for when the pause actually happens/finishes, so I can do the seek... And finally I can tell it to play.

Is there something I've missed in the API that would make this simpler? If not, maybe a Player.playAt(seconds) call could be added, or something?

Tero
Author of jQuery Tools and this website + JavaScript developer of Flowplayer.

Posts: 1656

Registered:
Nov 16, 2007

» Starting play at a particular location with pseudo-streaming?

Posted: Jan 22, 2009

Reply to: Starting play at a particular location with pseudo-streaming?, from ezran
Well. A hack indeed. You may be interested on this demo

http://flowplayer.org/demos/streaming/first-frame.html

a "slightly" less complicated.

(my 800th post by the way)

ezran

Posts: 14

Registered:
Jan 21, 2009

» » Starting play at a particular location with pseudo-streaming?

Posted: Jan 22, 2009

Reply to: » Starting play at a particular location with pseudo-streaming?, from tipiirai
The problem with using the clip 'start' property is that it changes the timeline, making it appear as if there's nothing before that in the video. I'd like to be able to jump to a spot 30 seconds (or even 30 minutes) into the video, but still allow the user to go back to the beginning -- they need to see exactly where in the video they are.

Imagine you've got the video on the page, and then a bunch of links/buttons below it, "play at 0:30", "play at "0:52", "play at 2:17", etc... Basically I want to be able to jump straight to various points in the video at any given time, triggered by javascript.

Thoughts?

Anssi
Flowplayer Flash & video streaming developer

Posts: 818

Registered:
Jul 24, 2007

» » » Starting play at a particular location with pseudo-streaming?

Posted: Jan 22, 2009

Reply to: » » Starting play at a particular location with pseudo-streaming?, from ezran
You can do this by scripting it by JavaScript. Try with adding just a onStart listener that immediately seeks to your desired position.

ezran

Posts: 14

Registered:
Jan 21, 2009

» » » » Starting play at a particular location with pseudo-streaming?

Posted: Jan 22, 2009

Reply to: » » » Starting play at a particular location with pseudo-streaming?, from Anssi
I tried that before coming up with the function I posted above.

However, since the player will only seek when it's in the PlayingState or PausedState, not during WaitingState or BufferingState, you can't just seek during the onStart function. (Because at that point, the player is in the BufferingState.)

It would be a very welcome addition to add the seeking ability to the BufferingState, though. It would take a good chunk of the "hackiness" out of the hack.

Anssi
Flowplayer Flash & video streaming developer

Posts: 818

Registered:
Jul 24, 2007

» » » » » Starting play at a particular location with pseudo-streaming?

Posted: Jan 22, 2009

Reply to: » » » » Starting play at a particular location with pseudo-streaming?, from ezran
Thanks for digging this up so deep into the code. I'll be checking this for our upcoming 3.1 release.

And sorry for not seeing the posts above .... :-(

ezran

Posts: 14

Registered:
Jan 21, 2009

» » » » » » Starting play at a particular location with pseudo-streaming?

Posted: Jan 27, 2009

Reply to: » » » » » Starting play at a particular location with pseudo-streaming?, from Anssi
So... I've done some more work on this.

I went ahead and added the seekTo call to the BufferingState.as:


95a95,97
>    internal override function seekTo(seconds:Number):void {
>      onEvent(ClipEventType.SEEK, getMediaController(), [seconds]);
>    }

However, I noticed another issue -- that you couldn't always seekTo a particular point in the video, let it play a while, and then seekTo that point again. (It definitely wouldn't work with the first point you try to seek to, but often if you seek to another point after that, then it would work as expected from then on.)

So I found and commented out this line in seek() inside NetStreamControllingStreamProvider.as:


157c157
<      if (Math.abs(seconds - _seekTarget) < 1) return;
---
>      //if (Math.abs(seconds - _seekTarget) < 1) return;

After making that change, the seeking was reliable, but I'm not sure if that could cause problems with anything else... I have yet to see a situation when I actually want it to skip out of seek() early there, though...

Anyway, with those two changes in place, and dealing with the situation that comes up when the video has ended, but you want to seek back into it and play, I was able to update my function to look like this:


    function seekVideo(seconds) {
      var p = $f();
      if (p.getState() == 1) {
        _resumeAndSeek(p, seconds);
      } else if (p.isLoaded()) {
        p.seek(seconds);
        if (!p.isPlaying()) p.play(); 
      } else {
        _loadAndSeek(p, seconds);
      }
    }
    
    function _resumeAndSeek(p, seconds) {
      var initialLoad = true;
      p.onStart(function () {
        if (!initialLoad) return; // Make sure we don't do this for future onStarts.
        initialLoad = false;
        p.seek(seconds); 
        if (!p.isPlaying()) p.play(); 
      });
      p.play();
    }

    function _loadAndSeek(p, seconds) {
      var initialLoad = true;
      p.onStart(function () {
        if (!initialLoad) return; // Make sure we don't do this for future onStarts.
        initialLoad = false;
        p.seek(seconds); 
        if (!p.isPlaying()) p.play(); 
      });
      p.load();
    }

I plan to refactor that a bit, of course, but this is fairly clear. With these changes, it more or less works, although there are slight delays and the timeline position indicator jumps around a bit during certain types of seeking.

Anssi
Flowplayer Flash & video streaming developer

Posts: 818

Registered:
Jul 24, 2007

» » » » » » » Starting play at a particular location with pseudo-streaming?

Posted: Jan 29, 2009

Reply to: » » » » » » Starting play at a particular location with pseudo-streaming?, from ezran
I have added your changes to the main codebase. It would be nice if you could test with development version, just to make sure that this will work also in the official FP version.

ezran

Posts: 14

Registered:
Jan 21, 2009

» » » » » » » » Starting play at a particular location with pseudo-streaming?

Posted: Feb 17, 2009

Reply to: » » » » » » » Starting play at a particular location with pseudo-streaming?, from Anssi
I just tested with the new 3.0.5, and it seems to be working great.

In case anyone's interested, here's the latest iteration of the seekVideo stuff, which also allows to jump to any video in the playlist:


var currentPlaylistIndex = -1;
function seekVideo(playlistIndex,seconds) {
  var p = $f();
  // If the video is done playing, or not yet loaded, we have to 
  // give it a kickstart.
  if (p.getState() == 1 || !p.isLoaded() || playlistIndex != currentPlaylistIndex) {
    _startAndSeek(p, playlistIndex, seconds);
  } else {
    p.seek(seconds);
    if (!p.isPlaying()) p.play();
  }
}

function _startAndSeek(p, playlistIndex, seconds) {
  var initialLoad = true;
  // Add a callback to do the actual seeking after the player
  // loads and we've got enough of the video buffered to know
  // where the seekpoints are.
  p.onStart(function () {
    if (!initialLoad) return; // Make sure we don't do this for future onStarts.
    initialLoad = false;
    p.seek(seconds); 
    if (!p.isPlaying()) p.play();
  });
  if (p.isLoaded()) {
    p.play(playlistIndex);
    currentPlaylistIndex = playlistIndex;
  } else {
    p.load(function () { p.play(playlistIndex); currentPlaylistIndex = playlistIndex });
  }
}

streamingvideo.pro
French multimedia developper

Posts: 37

Registered:
Dec 18, 2008

» » » » » » » » » Starting play at a particular location with pseudo-streaming?

Posted: 2 weeks and 2 days ago

Reply to: » » » » » » » » Starting play at a particular location with pseudo-streaming?, from ezran
Hello

Have you an example ?

my player don't start after
seekVideo(3,24)

thanks