如何从ExoPlayer播放列表中播放特定的视频Android?

ubby3x7f  于 2023-02-14  发布在  Android
关注(0)|答案(3)|浏览(259)

我正在使用ExoPlayer播放视频列表作为播放列表:

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);

它工作正常。但根据要求,我必须播放一个视频在一个特定的位置从列表时,点击。我怎么才能做到这一点?
谢谢!

55ooxyrt

55ooxyrt1#

您可能需要使用seekTo(窗口索引,位置M)。

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

3j86kqsm2#

下面是一个示例代码,它在单击时播放列表中特定位置的视频。

//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() {

        }
    });
6jjcrrmo

6jjcrrmo3#

如何使用JAVA android studio项目在ExoPlayer中播放播放列表中的特定视频

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();

相关问题