Use ERROR_CODE_BEHIND_LIVE_WINDOW instead of instanceof checks
PiperOrigin-RevId: 375514509
This commit is contained in:
parent
afe4217c1c
commit
6a8b9557cc
@ -34,6 +34,7 @@ import androidx.appcompat.app.AppCompatActivity;
|
||||
import com.google.android.exoplayer2.C;
|
||||
import com.google.android.exoplayer2.ExoPlaybackException;
|
||||
import com.google.android.exoplayer2.MediaItem;
|
||||
import com.google.android.exoplayer2.PlaybackException;
|
||||
import com.google.android.exoplayer2.Player;
|
||||
import com.google.android.exoplayer2.RenderersFactory;
|
||||
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.MediaCodecUtil.DecoderQueryException;
|
||||
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.MediaSourceFactory;
|
||||
import com.google.android.exoplayer2.source.TrackGroupArray;
|
||||
@ -413,20 +413,6 @@ public class PlayerActivity extends AppCompatActivity
|
||||
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 {
|
||||
|
||||
@Override
|
||||
@ -439,7 +425,7 @@ public class PlayerActivity extends AppCompatActivity
|
||||
|
||||
@Override
|
||||
public void onPlayerError(@NonNull ExoPlaybackException e) {
|
||||
if (isBehindLiveWindow(e)) {
|
||||
if (e.errorCode == PlaybackException.ERROR_CODE_BEHIND_LIVE_WINDOW) {
|
||||
player.seekToDefaultPosition();
|
||||
player.prepare();
|
||||
} else {
|
||||
|
@ -130,11 +130,12 @@ Available configuration values are:
|
||||
If automatic playback speed adjustment is not desired, it can be disabled by
|
||||
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
|
||||
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
|
||||
errors by resuming playback at the default position. The [PlayerActivity][] of
|
||||
the demo app exemplifies this approach.
|
||||
@ -142,7 +143,7 @@ the demo app exemplifies this approach.
|
||||
~~~
|
||||
@Override
|
||||
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.
|
||||
player.seekToDefaultPosition();
|
||||
player.prepare();
|
||||
@ -150,20 +151,6 @@ public void onPlayerError(ExoPlaybackException e) {
|
||||
// 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}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user