Android Exoplayer SimpleExoPlayerView播放/暂停事件
有没有办法钩住SimpleExoPlayerView控件的播放/暂停单击事件?我正在尝试将chromecast连接到我的游戏活动,我需要能够响应游戏事件,以确定exoplayer是应该播放内容,还是应该将内容发送到chromecast 我认为onPlayerStateChanged可能会有帮助,但我看不到一个ExoPlayer.STATE\u播放状态 试试这个解决方案:Android Exoplayer SimpleExoPlayerView播放/暂停事件,android,exoplayer2.x,Android,Exoplayer2.x,有没有办法钩住SimpleExoPlayerView控件的播放/暂停单击事件?我正在尝试将chromecast连接到我的游戏活动,我需要能够响应游戏事件,以确定exoplayer是应该播放内容,还是应该将内容发送到chromecast 我认为onPlayerStateChanged可能会有帮助,但我看不到一个ExoPlayer.STATE\u播放状态 试试这个解决方案: SimpleExoPlayerView simpleExoPlayerView = (SimpleExoPlayerView)
SimpleExoPlayerView simpleExoPlayerView = (SimpleExoPlayerView)findViewById(R.id.simpleExoView);
simpleExoPlayerView.setControlDispatcher(new PlaybackControlView.ControlDispatcher() {
@Override
public boolean dispatchSetPlayWhenReady(ExoPlayer exoPlayer, boolean b) {
// implement what you need
return b;
}
@Override
public boolean dispatchSeekTo(ExoPlayer exoPlayer, int i, long l) {
return false;
}
});
在XML中
从理论上讲,这应该可以解决我的问题,但它并不漂亮。我希望在chromecast演示应用程序中实现类似于状态机的东西。除此之外,这只是部分有效。演示应用程序在启动过程中使用ExoPlayer.setPlayWhenReady,但这绕过了我们设置的SimpleExoPlayerView dispatchSetPlayWhenReady。dispatchSetPlayWhenReady被点击的唯一时间是在最初加载并点击播放按钮之后。我没有看到SimpleExoPlayerView.setPlayWhenReady会发送到底层的ExoPlayerOk,我必须通过SimpleExoPlayerView源代码进行挖掘,但我想我找到了解决方法。播放按钮似乎使用mSimpleExoPlayerView.dispatchMediaKeyEvent(新的KeyEvent(KeyEvent.ACTION\u DOWN,KeyEvent.KEYCODE\u MEDIA\u play));它将委托给controlDispatcher.setPlayWhenReady
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bgFrag">
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/playerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
app:use_controller="true"
app:controller_layout_id="@layout/item_player_controler"
app:keep_content_on_player_reset="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:resize_mode="zoom"
app:shutter_background_color="@color/transperent">
</com.google.android.exoplayer2.ui.PlayerView>
<ImageView
android:id="@+id/ivExoPlayPause"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_play" />
</<androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:visibility="visible"
android:layout_height="match_parent">
<ImageView android:id="@id/exo_play"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:button="@null"
android:scaleType="centerInside"
/>
<ImageView android:id="@id/exo_pause"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:visibility="visible"
android:button="@null"
android:scaleType="centerInside"
/>
</FrameLayout>
videoviewholder.playerview.findViewById(R.id.exo_play).setOnClickListener(view->{
ToastUtils.showShort("play");
videoviewholder.videoItemBinding.ivExoPlayPause.setVisibility(View.INVISIBLE);
videoviewholder.videoItemBinding.playerview.getPlayer().setPlayWhenReady(true);
});
videoviewholder.playerview.findViewById(R.id.exo_pause).setOnClickListener(view->{
ToastUtils.showShort("pause");
videoviewholder.videoItemBinding.ivExoPlayPause.setVisibility(View.VISIBLE);
videoviewholder.videoItemBinding.playerview.getPlayer().setPlayWhenReady(false);
});
videoviewholder.playerview.setControllerAutoShow(true);
videoviewholder.playerview.setControllerShowTimeoutMs(0);
videoviewholder.playerview.setControllerHideOnTouch(false);
videoviewholder.playerview.setPlayer(player);