Tony Anecito
Tony Anecito

Reputation: 367

Youtube video stops after 1 second when using YouTubePlayerSupportFragment

I am using YouTubePlayerSupportFragment and after a second it stops playing.

If I move the slider it again starts to show a movie image and sound but stops again after less then a second. It almost looks as if the movie is not streaming and buffering but view is showing the movie controls and the image.

I am using the default video mode and here is my fragment class:

public class PlayerYouTubeFrag extends YouTubePlayerSupportFragment {

    String developerKey = "dev_key";
    private YouTubePlayer activePlayer;

    public static PlayerYouTubeFrag newInstance(String url) {

        PlayerYouTubeFrag playerYouTubeFrag = new PlayerYouTubeFrag();

        Bundle bundle = new Bundle();
        bundle.putString("url", url);

        playerYouTubeFrag.setArguments(bundle);
        playerYouTubeFrag.init();

        return playerYouTubeFrag;
    }

    private void init() {

        initialize(developerKey, new OnInitializedListener() {

            @Override
            public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) {
                System.out.println("PlyerYouTubeFrag initialization failure");
            }

            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
                activePlayer = player;
                if (getArguments().getString("url") != null) {
                    if (wasRestored) {
                        activePlayer.play();
                    } else {                         

                      activePlayer.loadVideo(getArguments().getString("url"), 0);
                    }
                }
            }
        });
    }
}

I am using viewpager for other fragments and here is the section of my main activity in my getItem() call where I create the fragment and return it:

if(ft3 ==null) ft3 = PlayerYouTubeFrag.newInstance(video_id);

for my main activity I use this xml

<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />

I am using 1.2.2 of the youtube api

I am running on 5.1 of android and have tried the newest and older youtube player. I have read about this might be happening for others and is pointed to being a API issue but I am not sure. Any ideas I am willing to try.

Upvotes: 4

Views: 1560

Answers (2)

Loic L.
Loic L.

Reputation: 61

I had the same problem and after few hours I finally found a solution. The problem is the view is probably not at the top of the layout where you want to put this youtubePlayerFragment.

That is why the video is cut after 1 second. To solve that you need to be sure that the place where you want to add the youtubeFragment is the top of the layout.

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.96"
        android:paddingTop="5dp"
        android:id="@+id/youtubeFragment">

</FrameLayout>

I do not really understand why you cannot invoke youtubePlayerFragment where you want. Perhaps to avoid some programmers to hide some parts of the video (for example artificially displaying sound without the view which is not legal following the conditions of use)...

Upvotes: 6

Daniel Vivek
Daniel Vivek

Reputation: 1

You cannot use youtubePlayerFragment as the view overlaps the youtubePlayerView, Youtube docs recommend to use another approach for overlaying views.

Please follow the steps in this guide for ActionBarDemoActivity

https://github.com/youtube/yt-android-player

Upvotes: -3

Related Questions