簡體   English   中英

如何在回收站視圖中處理 exo 播放器?

[英]How to handle exo players in recycler view?

我們如何在回收站視圖中使用多個 exo 播放器? 我看到了很多這樣的帖子和視頻,都使用了自定義回收器視圖類。 我可以繼續這樣做。 但是,我想制作一個完整的 Insta 克隆,我可以在其中發送 gif、圖像和視頻。 現在,當我使用自定義回收器視圖類時,我不知道如何使用 gif 和圖像播放視頻。 這是我的回收站查看項目代碼:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_margin="10dp"
    app:cardCornerRadius="6dp"
    app:cardElevation="1dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="10dp">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <com.sambhav2358.facebookclone.customviews.RoundedImageView
                android:id="@+id/senderImage"
                android:layout_width="50dp"
                android:layout_height="50dp"
                app:cornerRadius="100dp"
                android:src="@color/primary"/>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical"
                android:layout_marginStart="10dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/senderName"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:text="Sender name"
                        android:ellipsize="end"
                        android:textStyle="bold"
                        android:textColor="@color/black"
                        android:layout_weight="1"/>

                    <TextView
                        android:id="@+id/dateAdded"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Date added"
                        android:ellipsize="end"
                        android:layout_marginStart="10dp"/>

                </LinearLayout>

                <TextView
                    android:id="@+id/senderUserName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Sender user name"
                    android:ellipsize="end"
                    android:textColor="@color/black"
                    android:layout_weight="1"/>

            </LinearLayout>

            <ImageView
                android:id="@+id/more"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/more"
                android:layout_marginStart="5dp"
                android:padding="4dp"
                android:background="?selectableItemBackgroundBorderless"/>

        </LinearLayout>

        <TextView
            android:id="@+id/body"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Body\nOf\nThe\nPost"
            android:layout_margin="10dp"/>

        <androidx.cardview.widget.CardView
            android:id="@+id/postExtra"
            android:layout_width="fill_parent"
            android:layout_height="200dp"
            android:layout_margin="5dp"
            app:cardCornerRadius="6dp"
            app:cardElevation="0dp">

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

            <ImageView
                android:id="@+id/imageVIew"
                android:layout_width="match_parent"
                android:layout_height="200dp" />

            <com.giphy.sdk.ui.views.GifView
                android:id="@+id/gifView"
                android:layout_width="match_parent"
                android:layout_height="200dp"/>

        </androidx.cardview.widget.CardView>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.cardview.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:cardCornerRadius="50dp"
                android:backgroundTint="@color/primary">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingHorizontal="10dp"
                    android:paddingVertical="5dp"
                    android:gravity="center">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/like"
                        app:tint="@android:color/white" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Like"
                        android:textColor="@color/white"
                        android:layout_gravity="center"
                        android:layout_marginStart="10dp"/>

                </LinearLayout>

            </androidx.cardview.widget.CardView>

            <View
                android:layout_width="10dp"
                android:layout_height="0dp"/>

            <androidx.cardview.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:cardCornerRadius="50dp"
                android:backgroundTint="@color/primary">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingHorizontal="10dp"
                    android:paddingVertical="5dp"
                    android:gravity="center">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/share"
                        app:tint="@color/white" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Share"
                        android:textColor="@color/white"
                        android:layout_gravity="center"
                        android:layout_marginStart="10dp"/>

                </LinearLayout>

            </androidx.cardview.widget.CardView>

            <View
                android:layout_width="10dp"
                android:layout_height="0dp" />

            <androidx.cardview.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:cardCornerRadius="50dp"
                android:backgroundTint="@color/primary">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingHorizontal="10dp"
                    android:paddingVertical="5dp"
                    android:gravity="center">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/comment"
                        app:tint="@android:color/white" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Comment"
                        android:textColor="@color/white"
                        android:layout_gravity="center_vertical"
                        android:layout_marginStart="10dp"/>

                </LinearLayout>

            </androidx.cardview.widget.CardView>
        </LinearLayout>


    </LinearLayout>

</androidx.cardview.widget.CardView>

我知道了。 我知道了。 我明白了 我剛剛創建了一個創建靜態var? ExoPlayer類看起來像這樣:

import android.annotation.SuppressLint
import com.google.android.exoplayer2.ExoPlayer

@SuppressLint("StaticFieldLeak", "Deprecation")
class ExoPlayerPlaybackHelper {
    companion object{
        var playerView : ExoPlayer? = null
    }
}

然后,我在我的模型( data類)中使用它來創建一個帶有播放功能的ExoPlayer對象,最后,我的模型如下所示:

import com.google.android.exoplayer2.ExoPlayer
import com.sambhav2358.facebookclone.utils.ExoPlayerPlaybackHelper

data class Post(my params, var playerView: ExoPlayer?){
    fun playVideo(playerView : ExoPlayer) {
        ExoPlayerPlaybackHelper.playerView?.pause()

        ExoPlayerPlaybackHelper.playerView = playerView

        playerView.play()
    }

    constructor() : this(my params, null)
}

現在,在適配器中,我將我的 exo 播放器傳遞給我的數據類的對象,如下所示:

post.playerView = binding.videoView.player as ExoPlayer

最后一項任務是使用OnScrollListener根據滾動位置停止和播放視頻。 我使用了這段代碼,它為我完成了任務:

binding.recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {

                val pos = (recyclerView.layoutManager as? LinearLayoutManager)?.findLastCompletelyVisibleItemPosition()

                if (pos == -1) return

                Log.d(TAG, "onScrolled: Scrolled $pos")

                if (posts[pos!!].messageType == "video" || posts[pos].messageType == "messageVideo") { // you might remove this line If you only use video players. But, I needed it.
                    posts[pos].playVideo(posts[pos].playerView!!)
                    Log.d(TAG, "onScrolled: played")
                }


                Log.d(TAG, "onScrolled: startCheckPauseTask")
                for (position in posts.indices){
                    if (position == pos) continue

                    posts[position].playerView?.pause()
                    posts[position].playerView?.seekTo(0)
                }
                Log.d(TAG, "onScrolled: finishCheckPauseTask")

            }
        })

結果


您可以從這里查看結果

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM