Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setMediaItems can cause IndexOutOfBounds exception #86

Closed
danjcosg opened this issue Jun 13, 2022 · 1 comment
Closed

setMediaItems can cause IndexOutOfBounds exception #86

danjcosg opened this issue Jun 13, 2022 · 1 comment
Assignees
Labels

Comments

@danjcosg
Copy link

danjcosg commented Jun 13, 2022

Hi,
Calling media3.session.MediaController.setMediaItems(List mediaItems) can cause an IndexOutOfBoundsException when the input list is smaller than the current timeline & the current seek position is greater than the size of the new media items.

We're using the '1.0.0-alpha03' version of Media3, a snapshot build of a105d03

Steps to reproduce:

  1. set timeline to contain 4 media items: MediaController.setMediaItems(<List of 4 Media Items>)
  2. seek to the final media item: MediaController.seekTo(3, C.TIME_UNSET))
  3. Then try setting timeline to contain less than 4 new media items: MediaController.setMediaItems(<List of 3 or less new Items>)

Result:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: , PID: 9895
java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
at java.util.ArrayList.get(ArrayList.java:437)
at androidx.media3.session.MediaControllerImplBase.setMediaItemsInternal(MediaControllerImplBase.java:1887)
at androidx.media3.session.MediaControllerImplBase.setMediaItems(MediaControllerImplBase.java:888)
at androidx.media3.session.MediaController.setMediaItems(MediaController.java:942)
...

Cause:
In MediaControllerImplBase.java line 891, setMediaItemsInternal is called with resetToDefaultPosition as false and startIndex as C.INDEX_UNSET. This causes the start index for the newPositionInfo to be set to the current player index (MediaControllerImplBase.java line 1848), which will be out of bounds for the new, smaller list of media items.

Suggested fix:
I might just be using the function incorrectly, but it looks like the resetToDefaultPosition parameter in MediaControllerImplBase.java line 891 should just be true instead of false. This is in line with the function description in MediaController.java & avoids this pathway.

@tonihei
Copy link
Collaborator

tonihei commented Jun 15, 2022

Thanks for reporting, we'll provide a fix for this.

but it looks like the resetToDefaultPosition parameter in MediaControllerImplBase.java line 891 should just be true instead of false. This is in line with the function description in MediaController.java & avoids this pathway.

That's correct, this value should be true and the method should also handle the case when the current index is no longer in the allowed range even if resetToDefaultPosition is false.

@tonihei tonihei self-assigned this Jun 15, 2022
marcbaechinger added a commit that referenced this issue Jun 15, 2022
Issue: #86
#minor-release
PiperOrigin-RevId: 455182232
marcbaechinger added a commit that referenced this issue Jun 15, 2022
Issue: #86
#minor-release
PiperOrigin-RevId: 455182232
(cherry picked from commit 8f844b3)
@androidx androidx locked and limited conversation to collaborators Mar 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants