如何在 Exoplayer 中为下一个、上一个、倒回和前进添加监听器
How to add Listener for next , previous , rewind and forward in Exoplayer
我正在使用 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() 方法但是我想要每个按钮的侦听器以便我可以执行适当的操作??
谢谢
您可以添加 ExoPlayer.Listener()
并使用 PlaybackstateCompat
class 中的常量。
覆盖 onPlayerStateChanged
侦听器并使用 rewind/fastforward/etc
检查 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();
}
});
还有:
STATE_SKIPPING_TO_NEXT
STATE_SKIPPING_TO_PREVIOUS
我使用此 link 为上一个、下一个等自定义 ExoPlayer。您可以实现自己的逻辑来跟踪所有这些事件。对我有用。
我的做法
假设,我们已经创建了媒体源、DASH 或 HLS 的集合。
并使用带有 SimpleExoPlayerView
组件的播放器视图。它会自动有默认的控件,例如prev,next。
我使用当前 window 索引和此 exoPlayer.getCurrentWindowIndex()
来回处理媒体源的集合。
你可以通过这个onTracksChanged
方法
查看当前索引
@Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
currentIndex = exoPlayer.getCurrentWindowIndex();
}
你可以看看这个file来实现这些控制。
希望对您有所帮助。
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()
}
拦截 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)
}
}
我正在使用 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.Listener()
并使用 PlaybackstateCompat
class 中的常量。
覆盖 onPlayerStateChanged
侦听器并使用 rewind/fastforward/etc
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();
}
});
还有:
STATE_SKIPPING_TO_NEXT
STATE_SKIPPING_TO_PREVIOUS
我使用此 link 为上一个、下一个等自定义 ExoPlayer。您可以实现自己的逻辑来跟踪所有这些事件。对我有用。
我的做法
假设,我们已经创建了媒体源、DASH 或 HLS 的集合。
并使用带有 SimpleExoPlayerView
组件的播放器视图。它会自动有默认的控件,例如prev,next。
我使用当前 window 索引和此 exoPlayer.getCurrentWindowIndex()
来回处理媒体源的集合。
你可以通过这个onTracksChanged
方法
@Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
currentIndex = exoPlayer.getCurrentWindowIndex();
}
你可以看看这个file来实现这些控制。
希望对您有所帮助。
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()
}
拦截 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)
}
}