如何在Exoplayer中为“下一个”、“上一个”、“倒带”和“快进”添加监听器

5

我正在开发ExoPlayer,希望能够自定义ExoPlayer并监听下一个、上一个、快进、快退等事件,这样当用户点击“下一个”按钮时,将播放播放列表中的下一个视频,在使用“上一个”时,将播放播放列表中的上一个视频,以此类推。我正在使用自定义布局,它改变了设计但不能监听这些事件(下一个、上一个等)。以下是我的代码:-

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true">


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="200dp">

    <com.google.android.exoplayer2.ui.SimpleExoPlayerView
        android:id="@+id/player_view"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:resize_mode="fill"
        app:controller_layout_id="@layout/custom_controls"
        app:player_layout_id="@layout/custom_player_view"
        app:surface_type="texture_view"/>

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:visibility="gone"/>
</RelativeLayout>




<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_marginTop="220dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

我已经在ConcatenatingMediaSource中添加了Mediasource列表。它能够工作,但我还需要改变布局,因此我需要一个ExoPlayer中每个控件的监听器。我看到了这个链接。所以我知道当用户点击ExoPlayer中的下一首、上一首、快退、快进按钮时,onPositionDiscontinuity()方法会被调用。但我需要每个按钮的监听器,以便我可以执行适当的操作?
谢谢
6个回答

3

我使用这个链接来自定义 ExoPlayer 的上一个、下一个等功能。您可以实现自己的逻辑来跟踪所有这些事件。这对我很有效。


我们可以创建自己的PlaybackControlView类吗? - Andrain

3

我的方式

假设我们已经创建了DASH或HLS媒体源的集合。

并且使用带有SimpleExoPlayerView组件的播放器视图。 它将自动具有默认控件,例如prev,next。

我使用此exoPlayer.getCurrentWindowIndex()处理当前窗口索引的媒体源,以便前后切换媒体源。

您可以通过此onTracksChanged方法检查当前索引。

@Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {

currentIndex = exoPlayer.getCurrentWindowIndex();

}

您可以查看此文件以实现这些控件:file
希望这能帮到您。

由于 currentWindowIndex 已被弃用,请改用 currentMediaItemIndex。 - C-lio Garcia

0

关于倒带和快进功能,我认为这可能会对您有所帮助 >> 链接

我使用链接在ExoPlayer或ExoPlayer2中添加了倒带快进功能。您可以实现自己的逻辑来跟踪所有这些事件。这对我很有效。


虽然这个链接可能回答了问题,但最好在此处包含答案的基本部分并提供参考链接。如果链接页面更改,仅有链接的答案可能会失效。- 来自审查 - The Dreams Wind

0

拦截ExoPlayer控制UI事件的最佳方法是添加自定义调度程序。

mBinding.styledPlayerView.setControlDispatcher(CustomControlDispatcher())

class CustomControlDispatcher(): DefaultControlDispatcher() {

    override fun dispatchFastForward(player: Player): Boolean {
        Logger.d(TAG, "dispatchFastForward")
        return super.dispatchFastForward(player)
    }

    override fun dispatchRewind(player: Player): Boolean {
        Logger.d(TAG, "dispatchRewind")
        return super.dispatchRewind(player)
    }

}

1
ControlDispatcher及其子类现已从ExoPlayer中移除。 - ilansas
@ilansas 现在我们能为此做些什么? - Sumit Shukla
根据发布说明,现在您必须使用“ForwardingPlayer”来传递ExoPlayer实例,并覆盖其倒带和快进方法。 - ilansas

0
您可以添加一个 ExoPlayer.Listener() 并使用 PlaybackstateCompat 类中的常量。重写 onPlayerStateChanged 监听器并检查 playbackState 以进行倒带、快进等操作。
 player.addListener(new ExoPlayer.Listener() {
            @Override
            public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
                if (playbackState == PlaybackStateCompat.STATE_FAST_FORWARDING) {
                    //do something                 
                }
                if (playbackState == PlaybackStateCompat.STATE_REWINDING) {
                    //do something else               
                }
            }

            @Override
            public void onPlayWhenReadyCommitted() {

            }

            @Override
            public void onPlayerError(ExoPlaybackException error) {
                mExoPlayer.stop();

            }
        });

另外:

  1. STATE_SKIPPING_TO_NEXT
  2. STATE_SKIPPING_TO_PREVIOUS

-1
 if you have not customized exoplayer



    if (currentPlayingVodPosition-1 == player.currentWindowIndex){
 Toast.makeText(this,"prev clicked",Toast.LENGTH_SHORT).show()

                    }
if (currentPlayingVodPosition+1 == player.currentWindowIndex){
   Toast.makeText(this,"next clicked",Toast.LENGTH_SHORT).show()

                    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接