Skip to content

How to correctly remove PlayerMessage? #7278

Closed
@Kamil-H

Description

Searched documentation and description of the problem

I'm developing an app where I let user listen to some audio content. My content is built with ConcatenatingMediaSource and provided to the user as a playlist. There are advertisements baked into my content, HLS files. In the response from the API I have information when exactly the ad begins, when user reached middle of the ad and the end of the ad. I would like to be able to send send a data to the API in those points in timeline. I have checked all of the possible options in library like AdsMediaSource, ClippingMediaSource and nothing matches my requirement as well as PlayerMessage. It lets me set a "points" in my media files and be notified when user reached one.
I'm using following methods to set a time when message should be sent:

.setPayload(reportingData)
.setPosition(positionInPlaylist, (reportingData.position * SECONDS_TO_MILLISECONDS_MULTIPLIER).toLong())
.setDeleteAfterDelivery(false)
.setType(ADVERTISEMENT_MESSAGE_TYPE)
.send()

From what I observed, PlayerMessage is assigned to windowIndex (media on my playlist) and it's great, because when I for example add message to 4th windowIndex then remove 3rd item and I'm starting playing next one (the on that have message assigned), the message is delivered.
User can remove items in playlist and add them again. So ideally it would be to react on Timeline change and remove all of the messages and add it again, to make 100% sure that those are added in correct points of timeline.
My problem is that when I'm trying cancel a current messages:

playerMessages.forEach { it.cancel() }
playerMessages.clear()

and then I'm adding them again in current positions, I'm getting following error:

E/ExoPlayerImplInternal: Internal runtime error.
    java.lang.IndexOutOfBoundsException: Index: 14, Size: 1
        at java.util.ArrayList.get(ArrayList.java:437)
        at com.google.android.exoplayer2.ExoPlayerImplInternal.maybeTriggerPendingMessages(ExoPlayerImplInternal.java:1081)
        at com.google.android.exoplayer2.ExoPlayerImplInternal.updatePlaybackPositions(ExoPlayerImplInternal.java:553)
        at com.google.android.exoplayer2.ExoPlayerImplInternal.doSomeWork(ExoPlayerImplInternal.java:583)
        at com.google.android.exoplayer2.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:329)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:214)
        at android.os.HandlerThread.run(HandlerThread.java:67)

The error is not exactly related to what I'm doing, but is always happening when I'm deleting a MediaSource from the playlist that had PlayerMessage and I'm adding it again and trying to add its PlayerMessages

From what I can see in the crash report it says "IndexOutOfBoundsException: Index: 14, Size: 1", so it seems like the messages were removed from the list, but it somehow tries to iterate through them with higher index than the number of the items currently on the list.

Question

My question is: is it correct use case of the PlayerMessage? Maybe there is a better tool for this use case? Ideally it would be if I could add the "points" to MediaSource directly and be notified when user reached one, but I didn't find a way to do that.
It I'm not abusing PlayerMessage, then why I'm getting an error when trying to add them more?

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions