Skip to content

SeekTo throws IndexOutOfRange after adding item to DynamicConcatenatingMediaSource. #3407

@ourdex86

Description

@ourdex86

I am trying to use DynamicConcatenatingMediaSource to create dynamic playlists.

Here is the implementation.

public void initMusicPlayer(){

        if (songs !=null)
        {

            MediaSource mediaSource;
            ArrayList<MediaSource> sources = new ArrayList<>();

            MusicItem song;
            for (int i=0;i< songs.size();i++)
            {
                song = songs.get(i);
                mediaSource = buildMediaSource(Uri.parse(song.getMusicUrl()));
                sources.add(mediaSource);
            }

            dynamicMediaSource = new DynamicConcatenatingMediaSource();
            dynamicMediaSource.addMediaSources(sources);
            exoPlayer.prepare(dynamicMediaSource,false,false);

            exoPlayer.addListener(this);
            if (currentPlayingSongIndex == -1)
            {
                exoPlayer.setPlayWhenReady(false);
            }
            else
            {
                exoPlayer.seekTo(currentPlayingSongIndex,0);
                exoPlayer.setPlayWhenReady(true);
            }
        }
    }

public void addItemToPlaylist(MusicItem song,boolean shouldPlay){

        long position = exoPlayer.getContentPosition();
        MediaSource mediaSource = buildMediaSource(Uri.parse(song.getMusicUrl()));
        dynamicMediaSource.addMediaSource(mediaSource);
        exoPlayer.prepare(dynamicMediaSource,false,false);

        if (shouldPlay)
        {
            exoPlayer.seekTo(currentPlayingSongIndex,0);
        }
        else
        {
            exoPlayer.seekTo(currentPlayingSongIndex,position);
        }
    }

But, the audio is not played. Its immediately invoking onPlayerStateChanged with player state STATE_ENDED

Exoplayer Version : r2.5.4
Android OS Version : 7.1.2
Device : Pixel XL

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions