Getting IndexOutOfBoundsException crash in SinglePeriodTimeline
Issue description
Production users of my app are getting following crash sometimes. Following is the crash logs -:
Fatal Exception: java.lang.IndexOutOfBoundsException at com.google.android.exoplayer2.h.a.a(Assertions.java:68) at com.google.android.exoplayer2.source.y.a(SinglePeriodTimeline.java:186) at com.google.android.exoplayer2.source.h$b.a(ConcatenatingMediaSource.java:841) at com.google.android.exoplayer2.source.a.a(AbstractConcatenatedTimeline.java:176) at com.google.android.exoplayer2.a.a$b.a(AnalyticsCollector.java:778) at com.google.android.exoplayer2.a.a$b.a(AnalyticsCollector.java:724) at com.google.android.exoplayer2.a.a.a(AnalyticsCollector.java:424) at com.google.android.exoplayer2.i$a.a(ExoPlayerImpl.java:746) at com.google.android.exoplayer2.i.a(ExoPlayerImpl.java:681) at com.google.android.exoplayer2.i.a(ExoPlayerImpl.java:622) at com.google.android.exoplayer2.i.a(ExoPlayerImpl.java:567) at com.google.android.exoplayer2.i$1.handleMessage(ExoPlayerImpl.java:109) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:163) at android.app.ActivityThread.main(ActivityThread.java:6396) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
Reproduction steps
I am using Exoplayer DynamicConcatenatingMediaSource for prefetching next song in song queue. I am keeping only two sources at a time in DynamicConcatenatingMediaSource.As soon as I get the third source for adding in DynamicConcatenatingMediaSource, I remove the previous index source from DynamicConcatenatingMediaSource. stacktrace.txt
Following is the code snippet I'm using -:
public void onPositionDiscontinuity(int reason) {
int newIndex = exoPlayer.getCurrentWindowIndex();
if (newIndex != currentIndex) {
// The index has changed; update the UI to show info for source at
// newIndex
currentIndex = newIndex;
removeMediaSource(exoPlayer.getPreviousWindowIndex());
currentIndex = exoPlayer.getCurrentWindowIndex();
}
}
` private void removeMediaSource(int index) { if (index >= 0 && index < dynamicConcatenatingMediaSource.getSize()) { dynamicConcatenatingMediaSource.removeMediaSource(index, new Runnable() {
@Override
public void run() {
if (exoPlayer == null) {
return;
}
currentIndex = exoPlayer.getCurrentWindowIndex();
}
});
}
}`
Version of ExoPlayer being used
Exoplayer version - 2.8.2
Device(s) and version(s) of Android being used
Device-Redmi Note 5 Android-7.1.2 Device-Moto G(4) Plus Android-6.0
Please can you de-obfuscate the stack trace.
Please find below de-obfuscated stack trace.
Fatal Exception: java.lang.IndexOutOfBoundsException
at com.google.android.exoplayer2.util.Assertions.checkIndex(Assertions.java:68)
at com.google.android.exoplayer2.source.SinglePeriodTimeline.getPeriod(SinglePeriodTimeline.java:186)
at com.google.android.exoplayer2.source.ConcatenatingMediaSource$DeferredTimeline.getPeriod(ConcatenatingMediaSource.java:841)
at com.google.android.exoplayer2.source.AbstractConcatenatedTimeline.getPeriod(AbstractConcatenatedTimeline.java:176)
at com.google.android.exoplayer2.analytics.AnalyticsCollector$MediaPeriodQueueTracker.updateMediaPeriodToNewTimeline(AnalyticsCollector.java:778)
at com.google.android.exoplayer2.analytics.AnalyticsCollector$MediaPeriodQueueTracker.onTimelineChanged(AnalyticsCollector.java:724)
at com.google.android.exoplayer2.analytics.AnalyticsCollector.onTimelineChanged(AnalyticsCollector.java:424)
at com.google.android.exoplayer2.ExoPlayerImpl$PlaybackInfoUpdate.notifyListeners(ExoPlayerImpl.java:746)
at com.google.android.exoplayer2.ExoPlayerImpl.updatePlaybackInfo(ExoPlayerImpl.java:681)
at com.google.android.exoplayer2.ExoPlayerImpl.handlePlaybackInfo(ExoPlayerImpl.java:622)
at com.google.android.exoplayer2.ExoPlayerImpl.handleEvent(ExoPlayerImpl.java:567)
at com.google.android.exoplayer2.ExoPlayerImpl$1.handleMessage(ExoPlayerImpl.java:109)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6165)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
I am also facing the exactly same crash in my app in production on exoplayer version 2.8.2. This crash happens while adding or removing media in ConcatenatingMediaSource using addMediaSource or removeMediaSource. This crash is affecting a large portion of our users, is there a fix or workaround for this ?
Unfortunately, I can't reproduce the issue with the code above. Can you try to provide minimal reproduction steps in our demo app? Please also verify that you access ExoPlayer from the main thread only as this issue may have been caused by parallel access.
In any case, we will change something internally which will make the problem obsolete as the call to this method won't happen anymore.
Thanks for the quick reply. We are for sure accessing Exoplayer from main thread. Unfortunately i can't provide any reproduction steps as this crash comes at scale(when we release app). In which version are you planing to release the change where this method won't be called ?
Most likely 2.9.0, but we don't have a fixed date for that yet.
@tonihei - Can this issue be closed?
Yes, it's fixed (or rather not longer applicable) by above mentioned change.