ArrayIndexOutOfBoundsException in guessMediaTimeBasedOnElapsedRealTime
See original GitHub issueWe recently started using PlaybackStatsListener
in our app, and we’re seeing low-medium volume crashes that we haven’t been able to reproduce locally:
java.lang.ArrayIndexOutOfBoundsException: length=0; index=-1
at java.util.ArrayList.get(ArrayList.java:439)
at e.d.a.a.a.l(Unknown:5)
at com.google.android.exoplayer2.analytics.PlaybackStatsListener$PlaybackStatsTracker.guessMediaTimeBasedOnElapsedRealtime(PlaybackStatsListener:775)
at com.google.android.exoplayer2.analytics.PlaybackStatsListener$PlaybackStatsTracker.maybeUpdateMediaTimeHistory(PlaybackStatsListener:770)
at com.google.android.exoplayer2.analytics.PlaybackStatsListener$PlaybackStatsTracker.onEvents(PlaybackStatsListener:577)
at com.google.android.exoplayer2.analytics.PlaybackStatsListener.onEvents(PlaybackStatsListener:257)
at com.google.android.exoplayer2.analytics.AnalyticsCollector.lambda$setPlayer$1(AnalyticsCollector:137)
at com.google.android.exoplayer2.analytics.AnalyticsCollector.lambda$setPlayer$1$AnalyticsCollector(AnalyticsCollector:1)
at com.google.android.exoplayer2.analytics.-$$Lambda$AnalyticsCollector$4Pupd4CGdtbqprZwT2MGLeu1J-0.invoke(-:1)
at com.google.android.exoplayer2.util.ListenerSet$ListenerHolder.iterationFinished(ListenerSet:294)
at com.google.android.exoplayer2.util.ListenerSet.handleMessage(ListenerSet:239)
at com.google.android.exoplayer2.util.ListenerSet.lambda$eEvjP-IE0x3J2lRvKfFbbjRFRvc(ListenerSet:9)
at com.google.android.exoplayer2.util.-$$Lambda$ListenerSet$eEvjP-IE0x3J2lRvKfFbbjRFRvc.handleMessage(-:9)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7094)
at java.lang.reflect.Method.invoke(Method.java:-2)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
Examining the code, you can see how entering maybeUpdateMediaTimeHistory
in a playback state other than PLAYBACK_STATE_PLAYING
could lead to this crash:
private void maybeUpdateMediaTimeHistory(long realtimeMs, long mediaTimeMs) {
if (!keepHistory) {
return;
}
if (currentPlaybackState != PlaybackStats.PLAYBACK_STATE_PLAYING) {
if (mediaTimeMs == C.TIME_UNSET) {
return;
}
if (!mediaTimeHistory.isEmpty()) {
long previousMediaTimeMs = mediaTimeHistory.get(mediaTimeHistory.size() - 1)[1];
if (previousMediaTimeMs != mediaTimeMs) {
mediaTimeHistory.add(new long[] {realtimeMs, previousMediaTimeMs});
}
}
}
mediaTimeHistory.add(
mediaTimeMs == C.TIME_UNSET
? guessMediaTimeBasedOnElapsedRealtime(realtimeMs)
: new long[] {realtimeMs, mediaTimeMs});
}
private long[] guessMediaTimeBasedOnElapsedRealtime(long realtimeMs) {
long[] previousKnownMediaTimeHistory = mediaTimeHistory.get(mediaTimeHistory.size() - 1);
long previousRealtimeMs = previousKnownMediaTimeHistory[0];
long previousMediaTimeMs = previousKnownMediaTimeHistory[1];
long elapsedMediaTimeEstimateMs =
(long) ((realtimeMs - previousRealtimeMs) * currentPlaybackSpeed);
long mediaTimeEstimateMs = previousMediaTimeMs + elapsedMediaTimeEstimateMs;
return new long[] {realtimeMs, mediaTimeEstimateMs};
}
I’m happy to put up a PR to make the code more robust, but I’m wondering if this instead points to us using the analytics improperly in our app since I couldn’t find any other reports of this same crash.
- ExoPlayer version number: 2.14.0
- Android version: All (our minSDK is 19)
- Android device: Various
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
java.lang.ArrayIndexOutOfBoundsException? - Stack Overflow
Running your code gives me Exception for 3+ users. ArrayIndexOutOfBoundsException thrown at outputFile.print(namesArray[count4][count3]);.
Read more >Error Caused by java.lang.ArrayIndexOutOfBoundsException
lang.ArrayIndexOutOfBoundsException. I'm analyzing a 3-D 672x672x90 image. I am wondering, does the code has a maximum number of sample pixels ...
Read more >How to fix java.lang.ArrayIndexOutOfBoundsException? Java ...
Check out https://www.hellocodeclub.com for more tutorials and projects Learn how to fix the java error "java.lang.
Read more >6.7 Array in Java Tutorial With Example ... - YouTube
6.7 Array in Java Tutorial With Example ArrayIndexOutOfBoundsException. 104K views 7 years ago. Telusko. Telusko. 1.87M subscribers.
Read more >Order of exports when using dynamic import is altered from original ...
Order of exports when using dynamic import is altered from original source. Is this intentional? Does it follow some kind of spec? This...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We added a check in
maybeUpdateMediaTimeHistory
to avoid the error - see the linked commit above. We haven’t been able to repro locally to verify under what circumstances this error can be raised. Per my comment above, avoid using the same listener instance on two players in parallel (in case that happens).Closing the issue as a fix is submitted. Thanks.
Thank you, we’ll add an assert for this.