【问题标题】:Expoplayer shows black screen and not playing video in recycler viewExpoplayer 显示黑屏并且不在回收站视图中播放视频
【发布时间】:2021-02-04 10:41:04
【问题描述】:

我正在使用 Expoplayer 将视频显示为 recyclerview 中的第一个项目,我现在从 mp4 扩展中的 url 获取视频,它应该在屏幕显示时自动播放我还将整个布局包装在框架布局,因为有时我想在运行应用程序时隐藏视图,它显示黑屏,我不知道为什么!

这是我应该在哪里播放视频的viewholder 有人可以帮我吗?

class SpotlightViewHolder(val itemVw: View, val adapter: NewNewsAdapter) : RecyclerView.ViewHolder(itemVw), Player.EventListener {
lateinit var simpleExoplayer: SimpleExoPlayer
private var playbackPosition: Long = 0
private val dataSourceFactory: DataSource.Factory by lazy {
    DefaultDataSourceFactory(adapter.activity, "exoplayer-sample")
}

fun bind(spotLightData: List<SpotlightModel>?) {

    if (spotLightData == null) {
        itemVw.spotLayoutRoot.toggleVisibility(false)
    } else {
        val spotlight = spotLightData[0]

        itemVw.spotLayoutRoot.toggleVisibility(true)


        when (spotlight.adType) {
            "image" -> {
                itemVw.spot_light_image.toggleVisibility(true)
                itemVw.video_view.toggleVisibility(false)
                GlideApp.with(adapter.activity).load(spotLightData[0].coverUrl).into(itemVw.spot_light_image)
            }
            "video" -> {
                itemVw.spot_light_image.toggleVisibility(false)
                itemVw.video_view.toggleVisibility(true)
                initializePlayer(spotlight.coverUrl)

            }
        }
        itemVw.btn_spotLight_cta.text = spotlight.cta.title
        itemVw.btn_spotLight_cta.setOnClickListener {
            adapter.callback.spotLightClicked(spotLightData[0])
        }
    }
}

private fun initializePlayer(url: String) {
    Log.e(TAG, "initializePlayer: --------------------------------,-")
    var simpleExoplayer = SimpleExoPlayer.Builder(adapter.activity).build()
    preparePlayer(url)
    itemVw.video_view.player = simpleExoplayer
    simpleExoplayer.seekTo(playbackPosition)
    simpleExoplayer.playWhenReady = true
    simpleExoplayer.addListener(this)
}


private fun preparePlayer(videoUrl: String) {
    val uri = Uri.parse(videoUrl)
    val mediaSource = buildMediaSource(uri)
    simpleExoplayer.prepare(mediaSource)
}

override fun onPlayerError(error: ExoPlaybackException) {
    // handle error
    Log.e(TAG, "onPlayerError: -----------------------------${error}")
}


private fun buildMediaSource(uri: Uri): MediaSource {
    return ProgressiveMediaSource.Factory(dataSourceFactory)
            .createMediaSource(uri)
}


private fun releasePlayer() {
    playbackPosition = simpleExoplayer.currentPosition
    simpleExoplayer.release()
}}

这是布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="wrap_content"
>
<androidx.constraintlayout.widget.ConstraintLayout
     android:id="@+id/spotLayoutRoot"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.facebook.drawee.view.SimpleDraweeView
        android:layout_width="0dp"
        android:layout_height="230dp"
        android:id="@+id/spot_light_image"
        android:scaleType="centerCrop"
            tools:visibility="gone"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.exoplayer2.ui.SimpleExoPlayerView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="230dp"
        app:onlyShowVideo="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <Button
        android:id="@+id/btn_spotLight_cta"
        style="@style/btnStyle"
        android:layout_width="wrap_content"
        android:layout_height="32dp"
        android:background="@drawable/newswav_accent_button"
        android:backgroundTint="@color/white"
        android:text="Learn more"
        android:textColor="@color/red_death_covid"
        android:paddingStart="@dimen/small"
        android:paddingEnd="@dimen/small"
        android:layout_margin="@dimen/hiad_12_dp"
        android:stateListAnimator="@null"
        android:textAllCaps="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

    标签: android kotlin exoplayer exoplayer2.x


    【解决方案1】:

    只需使用您的 SimplePlayerView 实例添加这一行

    playerView.setKeepContentOnPlayerReset(true);
    

    或者,您也可以通过您的 layout.xml 文件执行此操作,

    app:keep_content_on_player_reset="true"
    

    【讨论】:

    • 它并没有真正的区别仍然显示黑屏
    • 我想你错过了将播放器绑定到播放器视图simpleExoPlayerView.setPlayer(player);
    • 你是说这一行吗 :: itemVw.video_view.player = simpleExoplayer
    • 奇怪的是我在另一个项目中尝试了相同的代码,但它工作正常该项目不包含recyclerview
    猜你喜欢
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    • 2019-05-30
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多