【问题标题】:YouTube Player Support Fragment crashes when user selects to view in full screen当用户选择全屏查看时,YouTube 播放器支持片段崩溃
【发布时间】:2019-11-18 22:10:54
【问题描述】:

我在自己的片段中而不是在活动中实现了 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)

【问题讨论】:

  • 错误信息是说你需要有一个默认的非参数构造函数——这就是 FragmentManager 在配置更改或进程死亡和重新创建后创建 Fragments 的方式。

标签: android kotlin android-youtube-api android-fullscreen


【解决方案1】:

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

【讨论】:

  • 您刚刚解决了这个问题 - 当您的进程终止并重新创建时,您仍然会遇到相同的异常(例如,如果您使用开发人员选项“不要保留”活动')
【解决方案2】:

YouTubePlayer 组件不适用于片段,您应该只为视频在活动中打开,但我的建议是在这里使用此组件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,我在片段中播放 YoutubePlayer 时遇到问题,但如果您需要将标签与其他视频一起放置,则不要工作,只有 Activity 的一名玩家
猜你喜欢
  • 2014-10-24
  • 2013-12-16
  • 1970-01-01
  • 1970-01-01
  • 2019-03-05
  • 2015-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多