ExoPlayer 2播放列表监听器 [英] ExoPlayer 2 Playlist Listener

查看:889
本文介绍了ExoPlayer 2播放列表监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ExoPlayer 2.x的新功能来播放音频文件列表,如下所示:

I'm using the new features from ExoPlayer 2.x to play a list of audio files like this:

List<MediaSource> playlist = new ArrayList<>();

...

ConcatenatingMediaSource concatenatedSource = new ConcatenatingMediaSource(
            playlist.toArray(new MediaSource[playlist.size()]));

mExoPlayer.prepare(concatenatedSource);
mExoPlayer.setPlayWhenReady(true);

这工作正常,但是为了相应地更新我的UI,我需要知道当前正在播放哪个曲目以及该曲目的进度.有来自ExoPlayer的听众吗?

This is working fine, but in order to update my UI accordingly, I need to know which track is currently playing and the progress of this track. Is there any listener from ExoPlayer?

谢谢!

推荐答案

所以我处于类似情况,需要知道播放列表中的下一个视频何时开始.我发现ExoPlayer.EventListener有一个名为onPositionDiscontinuity()的方法,该方法在每次视频更改或搜寻"到播放列表中的下一个视频时被调用.

So I am in a similar scenario and need to know when the next video in the playlist starts. I found that the ExoPlayer.EventListener has a method called onPositionDiscontinuity() that gets called every time the video changes or "seeks" to the next in the playlist.

我还没有广泛地使用这种方法,但是到目前为止,我仍然应该关注这种方法.触发该方法时不会传递任何参数,因此您必须保留某种计数器或队列以跟踪在任何给定时刻正在播放的内容.

I haven't played around with this method extensively, but from what I can see so far, this is the method that you should be concerned about. There are no parameters that get passed when the method is fired, so you'll have to keep some kind of counter or queue to keep track of whats being played at any given moment.

希望这会有所帮助!

Exoplayer.getCurrentWindowIndex()返回的索引更改为

change in index returned by Exoplayer.getCurrentWindowIndex() is the recommended way to detect item change in a playlist MediaSource.

int lastWindowIndex = 0; // global var in your class encapsulating exoplayer obj (Activity, etc.)

exoPlayer.addListener(new ExoPlayer.EventListener() {
        @Override
        public void onLoadingChanged(boolean isLoading) {
        }

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

        @Override
        public void onTimelineChanged(Timeline timeline, Object manifest) {
        }

        @Override
        public void onPlayerError(ExoPlaybackException error) {
        }

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

这篇关于ExoPlayer 2播放列表监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆