nadietp
nadietp

Reputation: 98

How to play a particular video from ExoPlayer playlist android?

I'm using ExoPlayer to play a list of videos as playlist:

    MediaSource[] mediaSources = new MediaSource[mList.size()];
    for (int i = 0; i < mList.size(); i++) {
        mediaSources[i] = buildMediaSource(Uri.parse(mList.get(i));
    }

    MediaSource mediaSource = mediaSources.length == 1 ? mediaSources[0]
            : new ConcatenatingMediaSource(mediaSources);

    mExoPlayer.prepare(mediaSource);

Its working fine. But as per requirement I have to play a video in a particular position from the list when click. How can I achieve this?

Thanks!

Upvotes: 7

Views: 7483

Answers (3)

Kartick
Kartick

Reputation: 1

How to play a particular video from the playlist in ExoPlayer using JAVA android studio project

ExoPlayer player;

player = new ExoPlayer.Builder(this).build(); //this is the latest version as of now
player.addMediaItems(mediaItemArrayList); //playlist has been added
player.seekToDefaultPosition(position);  //THIS IS WHAT YOU ARE LOOKING FOR

//player.setMediaItems(mediaItemArrayList,position,startingTime); //alternative way

player.prepare();
player.play();

Upvotes: 0

marcbaechinger
marcbaechinger

Reputation: 2789

You may want to use seekTo(windowIndex, positionMs).

player.prepare(mediaSource);
player.seek(3, C.TIME_UNSET);
player.setPlayWhenReady(true);

Upvotes: 18

Oyewo Remi
Oyewo Remi

Reputation: 426

Here is a sample code that plays video in a particular position from the list when click.

//Pass the position of the item on the listview/playlist from previous activity /fragment.
    Intent _intent = getIntent();
    position = _intent.getIntExtra("postion_id", 0);

    player = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector());

    playerView.setPlayer(player);

    DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory (this, Util.getUserAgent(this, "exo-demo"));

    ConcatenatingMediaSource concatenatingMediaSource = new ConcatenatingMediaSource();

    //Ensure to populate the allFiles array.

    for (int i = 0; i < allFiles.size(); i++) {

        File currentFile = new File(allFiles.get(i));

        MediaSource mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse("file://" + currentFile.getAbsolutePath()));
        concatenatingMediaSource.addMediaSource(mediaSource);

    }
    player.prepare(concatenatingMediaSource);
    //Play from the item selected on the playlist from previous activity/fragment
    player.seekTo(position, C.TIME_UNSET);
    player.setPlayWhenReady(true);


    player.addListener(new Player.EventListener() {
        @Override
        public void onTimelineChanged(Timeline timeline, Object manifest, int reason) {



        }

        @Override
        public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {

        }

        @Override
        public void onLoadingChanged(boolean isLoading) {

        }

        @Override
        public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {

        }

        @Override
        public void onRepeatModeChanged(int repeatMode) {

        }

        @Override
        public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {

        }

        @Override
        public void onPlayerError(ExoPlaybackException error) {

        }

        @Override
        public void onPositionDiscontinuity(int reason) {
            //THIS METHOD GETS CALLED FOR EVERY NEW SOURCE THAT IS PLAYED
            int latestWindowIndex = player.getCurrentWindowIndex();
            if (latestWindowIndex != position) {
                // item selected in playlist has changed, handle here
                position = latestWindowIndex;
                // ...
            }




        }

        @Override
        public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {

        }

        @Override
        public void onSeekProcessed() {

        }
    });

Upvotes: 5

Related Questions