Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to listen video position #3980

Closed
orhunkupeli7 opened this issue Mar 13, 2018 · 9 comments
Closed

How to listen video position #3980

orhunkupeli7 opened this issue Mar 13, 2018 · 9 comments
Assignees
Labels

Comments

@orhunkupeli7
Copy link

I'm using ExoPlayer 2.7.0 and I would like to listen player position in order to take actions. I couldn't find specific method for it. Could you please suggest me a proper solution?

Thanks

@tonihei
Copy link
Collaborator

tonihei commented Mar 13, 2018

You can query player.getCurrentPosition() as often as you like. The position is updated all the time, thus we can't send repeated updates in a callback. The progress bar in our UI module, for example, is updated by posting a message every 200ms.

@tonihei tonihei self-assigned this Mar 13, 2018
@orhunkupeli7
Copy link
Author

So, it is okay that to listen it with a handler.

@tonihei
Copy link
Collaborator

tonihei commented Mar 13, 2018

Yes, that's certainly possible.

@meikaiss
Copy link

Why not provide onProgressChangedListener? handler is Very troublesome.

@tonihei
Copy link
Collaborator

tonihei commented Mar 15, 2018

The problem with a onProgressChangedListener is that the position changes about every 10 milliseconds. Sending such a huge amount of callbacks would use much more CPU time (and thus battery) than polling the progress infrequently. Most progress bars don't have a resolution which would allow you to show a 10ms difference either, thus it also wouldn't make sense for most purposes.

Also, using a handler is not that troublesome:

public class ProgressTracker implements Runnable {
  
  private final Player player;
  private final Handler handler;

  public void ProgressTracker(Player player) {
    this.player = player;
    handler = new Handler();
    handler.post(this);
  }

  public void run() {
    long currentPosition = player.getCurrentPosition();
    handler.postDelayed(this, 200 /* ms */);
  }
}

@orhunkupeli7
Copy link
Author

public class ProgressTracker implements Runnable {

private final Player player;
private final Handler handler;
private PositionListener positionListener;
private final static int DELAY_MS = 1000;

protected ProgressTracker(Player player, PositionListener positionListener) {
    this.player = player;
    this.positionListener = positionListener;
    handler = new Handler();
    handler.post(this);
}

public void run() {
    int position = (int) player.getCurrentPosition();
    Log.e("Position::", String.valueOf(player.getCurrentPosition()));
    positionListener.progress(position);
    handler.postDelayed(this, DELAY_MS);
}

protected void purgeHandler() {
    handler.removeCallbacks(this);
}

}

I did like that. However output of the log is:
24358
24358
25088
25088
25357
25368
26088

Why there are duplicates how can I get rid of them

@tonihei
Copy link
Collaborator

tonihei commented Mar 16, 2018

I guess because either the position hasn't changed (for example while buffering) or you have multiple active ProgressTrackers.

In general, please direct these more generic programming question to StackOverflow or similar sites. This issue tracker is primarily for answering ExoPlayer specific questions.

@orhunkupeli7
Copy link
Author

thanks, sorry for inconvenience.

@tonihei
Copy link
Collaborator

tonihei commented Mar 26, 2018

Closing because question seems to be answered. Feel free to reopen if not.

@tonihei tonihei closed this as completed Mar 26, 2018
@google google locked and limited conversation to collaborators Aug 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants