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

南楼画角 提交于 2019-12-08 09:25:55

问题


I have implemented YouTubePlayerSupportFragment API inside my own fragment instead of in an activity. The video does play normally, but when the user selects to view the video in full screen from YouTube's provided media controls, the app orients to landscape, shows a black loading screen and then immediately crashes.

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

The ExpandedItemFragment does inflate normally when the video is not in full screen yet, but after the user selects it to be in full screen, the app crashes. The exception being thrown is:

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

The exception is located in my MainActivity.kt at

super.onCreate(savedInstanceState)

回答1:


The reason why my app was crashing when the user selects to put the youtube video into fullscreen mode was that the YouTubePlayerSupportFragment auto rotates to landscape mode. I wasn't handling configuration changes properly in the activity that inflated my fragment so it was having issues trying to re-inflate the fragment and it crashed my app. For anyone who has similar issues, I suggest either handling configuration changes manually or adding android:configChanges="orientation|keyboardHidden to whichever activity holds a fragment that inflates YouTubePlayerSupportFragment inside of it. The code I provided in my question will work for anyone who is trying to implement YouTubePlayerSupportFragment inside of a fragment rather than an activity.




回答2:


YouTubePlayer component doesn't work well with fragments, you should to open in a activity only for the video, but my advice to you is to use this component here https://github.com/PierfrancescoSoffritti/android-youtube-player

add to build.grandle

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

add to you XML

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

load video

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

This component implements web frame you don't need to generate an access key to use YoutTube API, negative point run advertise



来源:https://stackoverflow.com/questions/58923657/youtube-player-support-fragment-crashes-when-user-selects-to-view-in-full-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!