Problems using YouTubePlayerFragment, "YouTubePlayerView has a width of 0dp and 0dp"

I am really struggling with use YouTubePlayerFragmentin my application. My implementation is as follows:

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

Initialization is not a problem, as YouTubePlayer is displayed and the video is uploaded. But he immediately stops after one second with the following message:

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

This is the XML c layout FrameLayoutto which I am adding YouTubePlayerFragmentin the above code:

<?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>

I have no idea why YouTubePlayer pretends to have a width and height of 0. When I register the size of a FrameLayout, its height is 607 and its width is 1080 pixels. Any ideas how to solve this problem?

+4
source share

All Articles