Commit 4faf303c by tonihei Committed by Ian Baker

Add Player.getCurrentLiveOffset convenience method.

This returns the current offset to the live edge. The calculation is
non-intuitive enough to provide this convenience method.

PiperOrigin-RevId: 285171090
parent 0e1e4ad7
......@@ -24,6 +24,7 @@
* Add `SpannedSubject` to testutils, for assertions on
[Span-styled text]( https://developer.android.com/guide/topics/text/spans)
(e.g. subtitles).
* Add `Player.getCurrentLiveOffset` to conveniently return the live offset.
### 2.11.0 (2019-12-11) ###
......
......@@ -144,6 +144,19 @@ public abstract class BasePlayer implements Player {
}
@Override
public final long getCurrentLiveOffset() {
Timeline timeline = getCurrentTimeline();
if (timeline.isEmpty()) {
return C.TIME_UNSET;
}
long windowStartTimeMs = timeline.getWindow(getCurrentWindowIndex(), window).windowStartTimeMs;
if (windowStartTimeMs == C.TIME_UNSET) {
return C.TIME_UNSET;
}
return System.currentTimeMillis() - window.windowStartTimeMs - getContentPosition();
}
@Override
public final boolean isCurrentWindowSeekable() {
Timeline timeline = getCurrentTimeline();
return !timeline.isEmpty() && timeline.getWindow(getCurrentWindowIndex(), window).isSeekable;
......
......@@ -1001,6 +1001,19 @@ public interface Player {
boolean isCurrentWindowLive();
/**
* Returns the offset of the current playback position from the live edge in milliseconds, or
* {@link C#TIME_UNSET} if the current window {@link #isCurrentWindowLive() isn't live} or the
* offset is unknown.
*
* <p>The offset is calculated as {@code currentTime - playbackPosition}, so should usually be
* positive.
*
* <p>Note that this offset may rely on an accurate local time, so this method may return an
* incorrect value if the difference between system clock and server clock is unknown.
*/
long getCurrentLiveOffset();
/**
* Returns whether the current window is seekable, or {@code false} if the {@link Timeline} is
* empty.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment