Android Exoplayer SimpleExoPlayerView Play/Pause events



有没有办法挂接到SimpleExoPlayerView控件的播放/暂停点击事件?我正在尝试将 chromecast 连接到我的游戏活动,我需要能够响应播放事件,以确定 exoplayer 是应该播放内容还是应该将其发送到 chromecast。

我认为 onPlayerStateChanged 可能会对此有所帮助,但我看不到ExoPlayer.STATE_PLAYING状态。

in XML

<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>

item_player_controler.xml

<?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);

试试这个解决方案:

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;
}
});

最新更新