这是indexloc提供的服务,不要输入任何密码
Skip to content

setMediaItems can cause IndexOutOfBounds exception #86

@danjcosg

Description

@danjcosg

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.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions