问题
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!
回答1:
You may want to use seekTo(windowIndex, positionMs).
player.prepare(mediaSource);
player.seek(3, C.TIME_UNSET);
player.setPlayWhenReady(true);
回答2:
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() {
}
});
来源:https://stackoverflow.com/questions/45709105/how-to-play-a-particular-video-from-exoplayer-playlist-android