0

I just implement the code in this:

    public class PlayerYouTubeFrag extends YouTubePlayerSupportFragment {

    private String currentVideoID = "video_id";
    private YouTubePlayer activePlayer;

    public static PlayerYouTubeFrag newInstance(String url) {

    PlayerYouTubeFrag playerYouTubeFrag = new PlayerYouTubeFrag();

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

        playerYouTubeFrag.setArguments(bundle);

        return playerYouTubeFrag;
    }

    private void init() {

        initialize(DeveloperKey.DEVELOPER_KEY, new OnInitializedListener() {

            @Override
            public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) { }

            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
                activePlayer = player;
                activePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
                if (!wasRestored) {
                    activePlayer.loadVideo(getArguments().getString("url"), 0);

                }
            }
        });
    }

    @Override
    public void onYouTubeVideoPaused() {
        activePlayer.pause();
    }
}

And then calling an instance of the fragment like so:

PlayerYouTubeFrag myFragment = PlayerYouTubeFrag.newInstance("video_id");
getSupportFragmentManager().beginTransaction().replace(R.id.video_container, myFragment).commit();

Where video_container in my case was an empty frame layout.

I add a PlayerYouTubeFrag object into my ViewPager. Video is loaded , but when playing, it play only 0,01s and pause automatically. (p/s It's work well when I add the PlayerYouTubeFrag object into other view or video player in fullscreen mode).

Community
  • 1
  • 1

1 Answers1

0

The YouTube Player tends to pause itself if it is partially obstructed, check that the Player isn't slightly outside of the view port or has any overlapping views.

Scott Cooper
  • 2,779
  • 2
  • 23
  • 29