该问题的解决方法是使用一个自定义的ExoPlayerView和一个自定义的PlayerViewGestureDetector类。
首先在布局文件中,使用MotionLayout将自定义的ExoPlayerView嵌套。
可以使用以下代码实现自定义的ExoPlayerView:
public class CustomExoPlayerView extends FrameLayout {
private SimpleExoPlayer player;
private PlayerView playerView;
public CustomExoPlayerView(@NonNull Context context) {
super(context);
init();
}
public CustomExoPlayerView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomExoPlayerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
LayoutInflater.from(getContext()).inflate(R.layout.view_custom_exoplayer, this);
playerView = findViewById(R.id.player_view);
}
public void setPlayer(SimpleExoPlayer player) {
this.player = player;
playerView.setPlayer(player);
}
public SimpleExoPlayer getPlayer() {
return player;
}
public void setVideoURI(Uri uri) {
player.prepare(new ProgressiveMediaSource.Factory(new DefaultHttpDataSourceFactory("exoplayer")).createMediaSource(uri));
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (player != null) {
player.release();
}
}
}
然后在自定义的ExoPlayerView中使用自定义的PlayerViewGestureDetector类来监听手势操作,如下:
public class CustomExoPlayerView extends FrameLayout {
private SimpleExoPlayer player;
private PlayerView playerView;
private PlayerViewGestureDetector playerViewGestureDetector;
public CustomExoPlayerView(@NonNull Context context) {
super(context);
init();
}
public CustomExoPlayerView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomExoPlayerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs,