如何播放 ExoPlayer 播放列表 android 中的特定视频?
How to play a particular video from ExoPlayer playlist android?
我正在使用 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);
它工作正常。但是根据要求,我必须在单击时从列表中的特定位置播放视频。我怎样才能做到这一点?
谢谢!
您可能想要使用 seekTo(windowIndex, positionMs)。
player.prepare(mediaSource);
player.seek(3, C.TIME_UNSET);
player.setPlayWhenReady(true);
这是一个示例代码,当点击时在列表中的特定位置播放视频。
//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() {
}
});
我正在使用 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);
它工作正常。但是根据要求,我必须在单击时从列表中的特定位置播放视频。我怎样才能做到这一点?
谢谢!
您可能想要使用 seekTo(windowIndex, positionMs)。
player.prepare(mediaSource);
player.seek(3, C.TIME_UNSET);
player.setPlayWhenReady(true);
这是一个示例代码,当点击时在列表中的特定位置播放视频。
//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() {
}
});