当用户选择全屏观看时,YouTube 播放器支持片段崩溃

YouTube Player Support Fragment crashes when user selects to view in full screen

我在我自己的片段而不是 activity 中实现了 YouTubePlayerSupportFragment API。视频确实可以正常播放,但是当用户从 YouTube 提供的媒体控件中选择全屏观看视频时,该应用程序会转向横向,显示黑色加载屏幕,然后立即崩溃。

ExpandedItem.layout

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
 <LinearLayout
    android:id="@+id/youtube_layout_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/close_button">

    <fragment
        android:id="@+id/youtube_player_fragment"
        android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginTop="15dp"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

ExpandedItemFragment.kt

import android.os.Bundle
import android.text.SpannableString
import android.text.style.BulletSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.google.android.youtube.player.YouTubeInitializationResult
import com.google.android.youtube.player.YouTubePlayer
import com.google.android.youtube.player.YouTubePlayerSupportFragment
class ExpandedItemFragment(private val hasVideo: Boolean) : Fragment(), YouTubePlayer.OnInitializedListener {
    private lateinit var mosquitoVideo: YouTubePlayerSupportFragment

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(
            R.layout.expanded_item, container, false
        )
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        initializeLayout()
    }

    private fun initializeLayout() {
        if (hasVideo) {
           youTubeLayoutContainer.visibility = View.VISIBLE
           mosquitoVideo =
              childFragmentManager.findFragmentById(R.id.youtube_player_fragment) as YouTubePlayerSupportFragment
           mosquitoVideo.initialize(getString(R.string.google_api_key), this)
        }
    }
override fun onInitializationSuccess(
        provider: YouTubePlayer.Provider,
        youTubePlayer: YouTubePlayer,
        b: Boolean
    ) {
        if (!b) {
            youTubePlayer.cueVideo("rD8SmacBUcU")
        }
    }

    override fun onInitializationFailure(
        provider: YouTubePlayer.Provider,
        youTubeInitializationResult: YouTubeInitializationResult
    ) {
        Toast.makeText(activity, "Youtube video failed to play", Toast.LENGTH_SHORT).show()
    }

ExpandedItemFragment 在视频未全屏时正常展开,但在用户选择全屏后,应用程序崩溃。抛出的异常是:

Caused by: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.mosquitotracker.ExpandedItemFragment: could not find Fragment constructor 

异常位于我的 MainActivity.kt

super.onCreate(savedInstanceState)

YouTubePlayer 组件不能很好地处理片段,您应该只为视频打开 activity,但我建议您在这里使用此组件 https://github.com/PierfrancescoSoffritti/android-youtube-player

添加到build.grandle

dependencies {
     implementation 'com.pierfrancescosoffritti.androidyoutubeplayer:core:10.0.5'
        }

加你XML

<com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
        android:id="@+id/youtube_player_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       />

加载视频

YouTubePlayerView youTubePlayerView = findViewById(R.id.youtube_player_view);


youTubePlayerView.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
  @Override
  public void onReady(@NonNull YouTubePlayer youTubePlayer) {
    String videoId = "rD8SmacBUcU";
    youTubePlayer.loadVideo(videoId, 0);
  }
});

此组件实现了 web 框架,您无需生成访问密钥即可使用 YoutTube API,负分 运行 做广告

当用户选择将 YouTube 视频置于全屏模式时我的应用程序崩溃的原因是 YouTubePlayerSupportFragment 自动旋转到横向模式。我没有正确处理 activity 中的配置更改,它使我的片段膨胀,因此在尝试重新膨胀片段时遇到问题并使我的应用程序崩溃。对于遇到类似问题的任何人,我建议手动处理配置更改或将 android:configChanges="orientation|keyboardHidden 添加到任何 activity 包含的片段中,该片段会在其中膨胀 YouTubePlayerSupportFragment 。我在问题中提供的代码适用于任何试图在片段而不是 activity.

中实现 YouTubePlayerSupportFragment 的人