用户选择全屏观看时,YouTube Player支持片段崩溃 [英] YouTube Player Support Fragment crashes when user selects to view in full screen

查看:133
本文介绍了用户选择全屏观看时,YouTube Player支持片段崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在自己的片段中而不是在活动中实现了YouTubePlayerSupportFragment API.该视频确实可以正常播放,但是当用户从YouTube提供的媒体控件中选择以全屏方式查看视频时,该应用会定向为横向显示,显示黑色的加载屏幕,然后立即崩溃.

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

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

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会正常膨胀,但是在用户选择全屏显示后,应用程序崩溃了.抛出的异常是:

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 

该异常位于我的MainActivity.kt中,位于

The exception is located in my MainActivity.kt at

super.onCreate(savedInstanceState)

推荐答案

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

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.

这篇关于用户选择全屏观看时,YouTube Player支持片段崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆