Videoview(Exoplayer)在回收器视图中第一次播放,但当我向下滚动并上来时,视频无法播放。

问题描述 投票:0回答:1

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

很确定视频视图第二次没有被初始化。

我该怎么做才能让它正常工作?

还有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)
        }

                       }
android kotlin android-recyclerview android-videoview exoplayer
1个回答
1
投票

使用 前驱者 如果你想播放视频,在 回收站 当上下滚动时,它会自动保存实例并播放视频。

在gradle中添加这个

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

在xml中声明imageeview的地方添加exoplayer widget,如下所示

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

                       }
© www.soinside.com 2019 - 2024. All rights reserved.