android - 视频在播放前闪烁黑色 (ExoPlayer)

标签 android exoplayer exoplayer2.x android-video-player

我在 RecyclerView 中使用 ExoPlayer。
我现在的工作方式是:

  • 仅使用一个 ExoPlayer 实例
  • 当 RecyclerView 项进入 View 时,它准备播放器并将 PlayerView 附加到布局
  • 当 RecyclerView 项目消失时,它调用 setPlayer(null) 并从布局中删除 PlayerView

  • 但是,当滚动浏览我的 RecyclerView 时,每个视频在播放前都会非常短暂地闪烁(毫秒),如下面的视频所示:
    https://user-images.githubusercontent.com/30815862/128436800-d2153174-e768-4181-9fc0-19d9dec52efb.mp4
    我究竟做错了什么?我可以改变什么来防止这个黑屏在视频播放之前闪烁?
    以下是我的 RecyclerView 适配器的相关部分:
    public class PostAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        private Context context;
        private SimpleExoPlayer exoPlayer;
        private HlsMediaSource.Factory hlsMediaSourceFactory;
    
        public PostAdapter(Context context) {
            this.context = context;
            this.exoPlayer = new SimpleExoPlayer.Builder(context)
                    .build();
            this.exoPlayer.setVideoTextureView(new TextureView(context));
            this.hlsMediaSourceFactory = new HlsMediaSource.Factory(CustomMediaSourceFactory.buildMediaSourceFactory())
                    .setAllowChunklessPreparation(true); // TODO: Look into this
        }
    
        public class PostViewHolder extends RecyclerView.ViewHolder {
            public ConstraintLayout videoContainer;
            public ImageView videoThumbnail;
            public HlsMediaSource hlsMediaSource;
            public FrameLayout playerViewContainer;
            public PlayerView playerView;
            public Player.Listener playerListener;
    
            public PostViewHolder(View v) {
                super(v);
    
                videoContainer = v.findViewById(R.id.video_container);
                videoThumbnail = v.findViewById(R.id.video_thumbnail);
                playerViewContainer = v.findViewById(R.id.player_view_container);
    
                playerView = new PlayerView(context);
                playerView.setUseController(false);
                playerView.setKeepContentOnPlayerReset(true);
            }
    
            public void prepareVideo() {
                if (exoPlayer == null) {
                    return;
                }
    
                if (exoPlayer.getCurrentMediaItem() != hlsMediaSource.getMediaItem()) {
                    playerListener = new Player.Listener() {
                        @Override
                        public void onPlaybackStateChanged(int state) {
                            if (state == Player.STATE_READY) {
                                int index = playerViewContainer.indexOfChild(playerView);
                                if (index == -1) {
                                    // Add the player view to the layout
                                    playerViewContainer.addView(playerView);
                                    playerView.setVisibility(View.VISIBLE);
                                }
                            }
                        }
                    };
    
                    exoPlayer.addListener(playerListener);
    
                    exoPlayer.setVolume(0);
                    exoPlayer.setRepeatMode(Player.REPEAT_MODE_ALL);
    
                    // Set the media item to be played.
                    exoPlayer.setMediaSource(hlsMediaSource);
    
                    // Prepare the player
                    exoPlayer.prepare();
    
                    // Attach the player to the view
                    playerView.setPlayer(exoPlayer);
                }
    
                // Play
                playVideo();
            }
    
            public void playVideo() {
                if (exoPlayer != null) {
                    exoPlayer.play();
                }
            }
    
            // This is called when the RecyclerView item comes into view
            public void attachPlayer() {
                // Prepare the video
                prepareVideo();
            }
    
            // This is called when the RecyclerView item goes out of view
            public void detachPlayer() {
                playerView.setPlayer(null);
    
                if (playerListener != null) {
                    exoPlayer.removeListener(playerListener);
                }
    
                // Remove the player view from the layout
                if (playerView.getParent() != null) {
                    playerView.setVisibility(View.GONE);
                    ((ViewGroup) playerView.getParent()).removeView(playerView);
                }
            }
        }
    
        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            final Post post = (Post) data.get(position);
            Video video = post.getVideo();
    
            if (video != null) {
                Glide.with(context)
                        .load(video.getThumbnailUrl())
                        .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                        .into(holder.videoThumbnail);
    
                if (video.getUrls() != null && video.getUrls().getHls() != null) {
                    holder.hlsMediaSource = hlsMediaSourceFactory.createMediaSource(MediaItem.fromUri(video.getUrls().getHls()));
                }
            }
    
            holder.videoContainer.setVisibility(View.VISIBLE);
        }
    }
    
    以下是我布局的相关部分:
    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/video_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintDimensionRatio="H,16:9"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent">
    
            <com.makeramen.roundedimageview.RoundedImageView
                android:id="@+id/video_thumbnail"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
    
            <FrameLayout
                android:id="@+id/player_view_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
    任何建议表示赞赏!

    最佳答案

    我希望现在还为时不晚。检查您的播放器 View XML。确保其背景设置为透明。设置 app:shutter_background_color透明也可以解决它。

    关于android - 视频在播放前闪烁黑色 (ExoPlayer),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68674774/

    相关文章:

    android - ExoPlayer 视频不裁剪和适合

    android - Exoplayer2.x:​​ Player.STATE_ENDED 在视频结束时触发两次,如何在 android 中实现 exoplayer?

    android - ExoPlayer - 如何获取默认布局的源代码?

    java - Android 上的 lint 中的 MissingSuperCall 会跳过某些情况

    android - Exoplayer 搜索栏不适用于方向键

    android - Exoplayer 为 android 中的大型 mp4 文件抛出解码器初始化异常

    android - 'WindowManager.LayoutParams.FLAG_SECURE' 不处理 ExoPlayer2 Activity

    android - 如何使用 RecyclerView.scrollToPosition() 将位置移动到当前 View 的顶部?

    android - 对 EditText 施加约束

    android - 从不同的 Activity 访问共享首选项 (Android)