Videoview (Exoplayer) 在回收站视图中第一次播放,但是当我向下滚动并出现时,视频无法播放

Videoview (Exoplayer) in a recycler view plays for the first time but when I scroll down and come up the video fails to play

我正在尝试在显示图像和视频的片段中实现类似 Instagram 的回收站视图。图像工作正常。 videoview 第一次也能正常工作。但是当我向下滚动并且视频视图从显示的元素中移除然后向上滚动时,视频无法播放。

很确定 videoview 没有被第二次初始化。

我该怎么做才能让它发挥作用?

另外,onBindViewHolder 的具体调用时间是什么时候?

   class PostListAdapter(var posts: ArrayList<Post>) :
    RecyclerView.Adapter<PostListAdapter.PostViewHolder>() {


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = PostViewHolder(


        LayoutInflater.from(parent.context).inflate(com.example.mehfil.R.layout.item_post_v2, parent, false)

    )

    override fun getItemCount() = posts.size

    override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
        holder.bind(posts[position])


    }

我的绑定方法如下所示...

  fun bind(post: Post) {

        if(post.type!!equals("video")) {
            //trigger video
            imageView.visibility = View.GONE
            videoView.visibility = View.VISIBLE
            try {
                videoView.setSource("mp4 video link")
            } catch (E: Exception) {
                Log.d("Video Error", E.toString())
            }
        }
        else
        {
            videoView.visibility=View.GONE
            imageView.visibility=View.VISIBLE
            //trigger images
            if(post.media!= null)
        {
            if(post.media!!.indexOf("http:")!= -1)
                post.media=post.media!!.replace("http","https")
            imageView.loadImage(post.media , progressDrawable)
            Log.d("Media link ",post.media)
        }

                       }

Use exoplayer if you want to play videos in recyclerview , it automatically saves instance and play video when scrolled up or down

将此添加到 gradle

implementation 'com.google.android.exoplayer:exoplayer:2.10.8'

像这样在 xml 中声明 imageview 的地方添加 exoplayer 小部件

<com.google.android.exoplayer2.ui.PlayerView
                    android:id="@+id/exoPlayer"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:visibility="gone" />

现在你的方法看起来像这样使用 exoplayer

fun bind(post: Post) {

        if(post.type!!equals("video")) {
            //trigger video
            imageView.visibility = View.GONE
            exoplayer.visibility = View.VISIBLE
            try {
              val uri = Uri.parse(
                    video_path
                )
             val player = ExoPlayerFactory.newSimpleInstance(context)
                exoPlayer.setPlayer(player)
 // Produces DataSource instances through which media data is loaded.
                val dataSourceFactory: DataSource.Factory =
                    DefaultDataSourceFactory(
                        context,
                        Util.getUserAgent(context, "Appname")
                    )

                val videoSource: MediaSource = ProgressiveMediaSource.Factory(dataSourceFactory)
                    .createMediaSource(uri)
// Prepare the player with the source.
                // Prepare the player with the source.
                player.prepare(videoSource)
            } catch (E: Exception) {
                Log.d("Video Error", E.toString())
            }
        }
        else
        {
            exoplayer.visibility=View.GONE
            imageView.visibility=View.VISIBLE
            //trigger images
            if(post.media!= null)
        {
            if(post.media!!.indexOf("http:")!= -1)
                post.media=post.media!!.replace("http","https")
            imageView.loadImage(post.media , progressDrawable)
            Log.d("Media link ",post.media)
        }

                       }