我正在播放来自网址的视频,我想在视频开始播放时显示文字。文字可能像“1/6”。
我添加了Player.EventListener
,但它无效。
// Player Event Listener
Player.EventListener eventListener = new Player.EventListener() {
@Override
public void onTimelineChanged(Timeline timeline, Object manifest) {
}
@Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
}
@Override
public void onLoadingChanged(boolean isLoading) {
progressBar.setVisibility(isLoading ? View.VISIBLE : View.GONE);
}
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
if (playbackState == PlaybackStateCompat.ACTION_SKIP_TO_NEXT) {
Toast.makeText(VideoActivity.this, "next", Toast.LENGTH_SHORT).show();
}
if (playbackState == PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) {
Toast.makeText(VideoActivity.this, "previous", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onRepeatModeChanged(int repeatMode) {
}
@Override
public void onPlayerError(ExoPlaybackException error) {
}
@Override
public void onPositionDiscontinuity() {
}
@Override
public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
}
};
更新
onCreate Method
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
ButterKnife.bind(this);
presenter = new VideosPresenter(this);
presenter.getVideoUrls();
// 1. Create a default TrackSelector
bandwidthMeter = new DefaultBandwidthMeter();
videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
// 2. Create a default LoadControl
LoadControl loadControl = new DefaultLoadControl();
// 3. Create the player
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);
mediaDataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, getString(R.string.app_name)), null);
simpleExoPlayerView = findViewById(R.id.player_view);
simpleExoPlayerView.requestFocus();
simpleExoPlayerView.setPlayer(player);
presenter = new VideosPresenter(this);
player.addListener(eventListener);
}
初始化
private void initializePlayer() {
simpleExoPlayerView.requestFocus();
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(bandwidthMeter);
trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
simpleExoPlayerView.setPlayer(player);
MediaSource[] mediaSources = new MediaSource[urlList.size()];
for (int i = 0; i < urlList.size(); i++) {
mediaSources[i] = buildMediaSource(Uri.parse(urlList.get(i)));
}
MediaSource mediaSource = mediaSources.length == 1 ? mediaSources[0]
: new ConcatenatingMediaSource(mediaSources);
player.prepare(mediaSource);
}
提取的视频网址
@Override
public void onVideosFetched(String[] urls) {
if (urls.length == 0) {
showAlertAndFinish(getString(R.string.empty_video_message));
return;
}
urlList.addAll(new ArrayList<>(Arrays.asList(urls)));
initializePlayer();
/* if ((*//*Util.SDK_INT <= 23 ||*//* player == null)) {
initializePlayer();
}*/
}
答案 0 :(得分:2)
在调用initializePlayer()
方法后,您正在使用onVideosFetched(String[] urls)
方法重新初始化您的播放器。但是你还没有在那里分配eventListener。
private void initializePlayer() {
simpleExoPlayerView.requestFocus();
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(bandwidthMeter);
trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
simpleExoPlayerView.setPlayer(player);
MediaSource[] mediaSources = new MediaSource[urlList.size()];
for (int i = 0; i < urlList.size(); i++) {
mediaSources[i] = buildMediaSource(Uri.parse(urlList.get(i)));
}
MediaSource mediaSource = mediaSources.length == 1 ? mediaSources[0]
: new ConcatenatingMediaSource(mediaSources);
player.prepare(mediaSource);
//Add lister to the player
player.addListener(eventListener);
}
希望有所帮助:)
答案 1 :(得分:1)
您似乎使用了错误的播放状态常量。 ExoPlayer https://notepad-plus-plus.org/community/topic/14363/r-file-association-messed-up是Player.STATE_IDLE,Player.STATE_BUFFERING,Player.STATE_READY和Player.STATE_ENDED。
要检测玩家何时在时间轴中从一个项目转到另一个项目,您需要使用onPositionDiscontinuity而不是onPlaybackStateChanged。这样的事情可能会起到作用:
int lastWindowIndex;
@Override
public void onPositionDiscontinuity(int reason) {
int currentWindowIndex = player.getCurrentWindowIndex();
if (reason == Player.DISCONTINUITY_REASON_PERIOD_TRANSITION) {
if (lastPlaybackState == currentWindowIndex - 1) {
// skipped to next
} else if (lastPlaybackState == currentWindowIndex + 1) {
// skipped to previous
} else {
// jumped more than one window index
}
}
lastWindowIndex = currentWindowIndex;
startOrUpdateNotification();
}