mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +00:00
Use ERROR_CODE_BEHIND_LIVE_WINDOW instead of instanceof checks
PiperOrigin-RevId: 375514509
This commit is contained in:
parent
afe4217c1c
commit
6a8b9557cc
2 changed files with 6 additions and 33 deletions
|
|
@ -34,6 +34,7 @@ import androidx.appcompat.app.AppCompatActivity;
|
||||||
import com.google.android.exoplayer2.C;
|
import com.google.android.exoplayer2.C;
|
||||||
import com.google.android.exoplayer2.ExoPlaybackException;
|
import com.google.android.exoplayer2.ExoPlaybackException;
|
||||||
import com.google.android.exoplayer2.MediaItem;
|
import com.google.android.exoplayer2.MediaItem;
|
||||||
|
import com.google.android.exoplayer2.PlaybackException;
|
||||||
import com.google.android.exoplayer2.Player;
|
import com.google.android.exoplayer2.Player;
|
||||||
import com.google.android.exoplayer2.RenderersFactory;
|
import com.google.android.exoplayer2.RenderersFactory;
|
||||||
import com.google.android.exoplayer2.SimpleExoPlayer;
|
import com.google.android.exoplayer2.SimpleExoPlayer;
|
||||||
|
|
@ -43,7 +44,6 @@ import com.google.android.exoplayer2.ext.ima.ImaAdsLoader;
|
||||||
import com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.DecoderInitializationException;
|
import com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.DecoderInitializationException;
|
||||||
import com.google.android.exoplayer2.mediacodec.MediaCodecUtil.DecoderQueryException;
|
import com.google.android.exoplayer2.mediacodec.MediaCodecUtil.DecoderQueryException;
|
||||||
import com.google.android.exoplayer2.offline.DownloadRequest;
|
import com.google.android.exoplayer2.offline.DownloadRequest;
|
||||||
import com.google.android.exoplayer2.source.BehindLiveWindowException;
|
|
||||||
import com.google.android.exoplayer2.source.DefaultMediaSourceFactory;
|
import com.google.android.exoplayer2.source.DefaultMediaSourceFactory;
|
||||||
import com.google.android.exoplayer2.source.MediaSourceFactory;
|
import com.google.android.exoplayer2.source.MediaSourceFactory;
|
||||||
import com.google.android.exoplayer2.source.TrackGroupArray;
|
import com.google.android.exoplayer2.source.TrackGroupArray;
|
||||||
|
|
@ -413,20 +413,6 @@ public class PlayerActivity extends AppCompatActivity
|
||||||
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
|
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isBehindLiveWindow(ExoPlaybackException e) {
|
|
||||||
if (e.type != ExoPlaybackException.TYPE_SOURCE) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
Throwable cause = e.getSourceException();
|
|
||||||
while (cause != null) {
|
|
||||||
if (cause instanceof BehindLiveWindowException) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
cause = cause.getCause();
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private class PlayerEventListener implements Player.EventListener {
|
private class PlayerEventListener implements Player.EventListener {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -439,7 +425,7 @@ public class PlayerActivity extends AppCompatActivity
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPlayerError(@NonNull ExoPlaybackException e) {
|
public void onPlayerError(@NonNull ExoPlaybackException e) {
|
||||||
if (isBehindLiveWindow(e)) {
|
if (e.errorCode == PlaybackException.ERROR_CODE_BEHIND_LIVE_WINDOW) {
|
||||||
player.seekToDefaultPosition();
|
player.seekToDefaultPosition();
|
||||||
player.prepare();
|
player.prepare();
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -130,11 +130,12 @@ Available configuration values are:
|
||||||
If automatic playback speed adjustment is not desired, it can be disabled by
|
If automatic playback speed adjustment is not desired, it can be disabled by
|
||||||
setting `minPlaybackSpeed` and `maxPlaybackSpeed` to `1.0f`.
|
setting `minPlaybackSpeed` and `maxPlaybackSpeed` to `1.0f`.
|
||||||
|
|
||||||
## BehindLiveWindowException ##
|
## BehindLiveWindowException and ERROR_CODE_BEHIND_LIVE_WINDOW ##
|
||||||
|
|
||||||
The playback position may fall behind the live window, for example if the player
|
The playback position may fall behind the live window, for example if the player
|
||||||
is paused or buffering for a long enough period of time. If this happens then
|
is paused or buffering for a long enough period of time. If this happens then
|
||||||
playback will fail and a `BehindLiveWindowException` will be reported via
|
playback will fail and an exception with error code
|
||||||
|
`ERROR_CODE_BEHIND_LIVE_WINDOW` will be reported via
|
||||||
`Player.Listener.onPlayerError`. Application code may wish to handle such
|
`Player.Listener.onPlayerError`. Application code may wish to handle such
|
||||||
errors by resuming playback at the default position. The [PlayerActivity][] of
|
errors by resuming playback at the default position. The [PlayerActivity][] of
|
||||||
the demo app exemplifies this approach.
|
the demo app exemplifies this approach.
|
||||||
|
|
@ -142,7 +143,7 @@ the demo app exemplifies this approach.
|
||||||
~~~
|
~~~
|
||||||
@Override
|
@Override
|
||||||
public void onPlayerError(ExoPlaybackException e) {
|
public void onPlayerError(ExoPlaybackException e) {
|
||||||
if (isBehindLiveWindow(e)) {
|
if (e.errorCode == PlaybackException.ERROR_CODE_BEHIND_LIVE_WINDOW) {
|
||||||
// Re-initialize player at the current live window default position.
|
// Re-initialize player at the current live window default position.
|
||||||
player.seekToDefaultPosition();
|
player.seekToDefaultPosition();
|
||||||
player.prepare();
|
player.prepare();
|
||||||
|
|
@ -150,20 +151,6 @@ public void onPlayerError(ExoPlaybackException e) {
|
||||||
// Handle other errors.
|
// Handle other errors.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isBehindLiveWindow(ExoPlaybackException e) {
|
|
||||||
if (e.type != ExoPlaybackException.TYPE_SOURCE) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
Throwable cause = e.getSourceException();
|
|
||||||
while (cause != null) {
|
|
||||||
if (cause instanceof BehindLiveWindowException) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
cause = cause.getCause();
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
~~~
|
~~~
|
||||||
{: .language-java}
|
{: .language-java}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue