使用YouTubePlayerFragment的问题,“YouTubePlayerView宽0dp,高0dp”

时间:2016-05-27 10:16:52

标签: android android-youtube-api

我真的很难在我的应用程序中使用YouTubePlayerFragment。我的实现如下:

private void loadYouTubeVideo(final YouTubeViewHolder h, final Content content) {
        final YouTubePlayerFragment youTubePlayerFragment = YouTubePlayerFragment.newInstance();
        FragmentTransaction transaction = ((DetailActivity) getContext()).getFragmentManager().beginTransaction();
        transaction.add(R.id.fl_youtube, youTubePlayerFragment).commit();
        final ViewTreeObserver observer = h.mRlYouTube.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                youTubePlayerFragment.initialize(YOUTUBE_PLAYER_API_KEY, new YouTubePlayer.OnInitializedListener() {
                    @Override
                    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
                        if(!wasRestored) {
                            youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
                            youTubePlayer.loadVideo(content.getVideoId());
                        }
                    }

                    @Override
                    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
                        Log.e(getClass().getSimpleName(), "Error initializing YouTube Video with id ");
                        FragmentTransaction transaction = ((DetailActivity) getContext()).getFragmentManager().beginTransaction();
                        transaction.remove(youTubePlayerFragment);
                    }
                });
            }
        });
    }

初始化没有问题,因为会显示YouTubePlayer并加载视频。但它会在一秒后立即停止,并显示以下消息:

"YouTube video playback stopped due to the player's view being too small. The YouTubePlayerView is 0dp wide (minimum is 200dp) and 0dp high (minimum is 110dp)."

这是包含FrameLayout的XML布局,我在上面的代码中添加了YouTubePlayerFragment

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fl_youtube"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="200dp"
    android:minHeight="110dp"
    android:layout_marginBottom="5dp"
    >

</FrameLayout>

我不知道为什么YouTubePlayer假装宽度和高度为0.当我记录FrameLayout的大小时,它的高度为607,宽度为1080像素。任何想法如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

它对我有用:在您的android:configChanges="keyboardHidden|orientation|screenSize"活动描述中添加AndroidManifest.xml,如下所示:

<activity android:name=".MyActivity"
android:configChanges="keyboardHidden|orientation|screenSize">

或者只是

<activity android:name=".MyActivity"
    android:configChanges="orientation">

via