Pemba Tamang
Pemba Tamang

Reputation: 1052

Exoplayer Clear All MediaItems except the one that's playing

I am working on making dynamic playlist on exoplayer using the simple and convenient methods referencing this article. I want to clear all the media items except the one that is currently playing.

My code:

private fun clearExoplayer() {
        val queueSize = exoPlayer.mediaItemCount
        Timber.i("clearing items here quesize $queueSize")
        for (i in 0..queueSize) {
            if (i != exoPlayer.currentWindowIndex) {
                exoPlayer.removeMediaItem(i)
            }
        }


    }

but I am getting an error which I cannot resolve.

java.lang.IllegalArgumentException
        at com.google.android.exoplayer2.util.Assertions.checkArgument(Assertions.java:41)
        at com.google.android.exoplayer2.ExoPlayerImpl.removeMediaItemsInternal(ExoPlayerImpl.java:1221)
        at com.google.android.exoplayer2.ExoPlayerImpl.removeMediaItems(ExoPlayerImpl.java:460)
        at com.google.android.exoplayer2.BasePlayer.removeMediaItem(BasePlayer.java:71)
        at com.google.android.exoplayer2.SimpleExoPlayer.removeMediaItem(SimpleExoPlayer.java:1443)
        at in_.co.innerpeacetech.music.exoplayer.BKMediaManager.clearExoplayer(BKMediaManager.kt:315)

What could be the problem ??

Upvotes: 0

Views: 1946

Answers (2)

tfrysinger
tfrysinger

Reputation: 1336

I am not familiar with how Exoplayer manages this queue, but any time I see a loop that attempts to walk through a list and also changes the contents of that list it is suspicious. Basically you are deleting an item in the very same list that you got the original size from and stored in variable 'queueSize'. The first time you delete an item in the list, the queue is no longer queueSize, right? So won't you eventually end up using 'i' to reference a position that no longer exists?

I think you will need to keep track of which item in the queue is the one you want to keep, then "skip over it" as you are removing items. So there will be three steps:

  1. Find out where the item you want to keep is in the queue.
  2. Delete all the items up to that item
  3. Delete all the items after that item

Something along the lines of this pseudo-code:

// Find my item
int myItemOriginalIndex = -1;
for ( int x = 0; x < myList.size(); x++ ) {
    if ( MATCHING CONDITION TEST HERE ) {
        myItemOriginalIndex = x;
        break;
    }
}
// Delete up to my item - notice I am always deleting at position "0" 
// since presumably the list structure changes as I delete it
if ( myItemOriginalIndex > 0 ) {
    // This means the one I want to keep isn't the first one
    for ( int x = 0; x < myItemOriginalIndex; x++ {
        myList.remove(0);
    }
}
// Now the one I want to keep is the first one, delete the rest
// Notice we start with item 1, not item 0 since we want to keep that one!
for ( int x = 1; x < myList.size(); x++ ) {
    myList.remove(1);
}

The point is that when you are modifying the list itself, the technique is to continue to remove from the same position, as the list shrinks.

Upvotes: 1

Jaswant Singh
Jaswant Singh

Reputation: 10759

Try this

private fun clearExoplayer() {
    val queueSize = exoPlayer.mediaItemCount
    Timber.i("clearing items here quesize $queueSize")
    for (i in queueSize downTo 0) {
        if (i != exoPlayer.currentWindowIndex) {
            exoPlayer.removeMediaItem(i)
        }
    }


}

Upvotes: 1

Related Questions