Search in sources :

Example 1 with YouTubePlayerStateListener

use of com.winsonchiu.reader.utils.YouTubePlayerStateListener in project Reader by TheKeeperOfPie.

the class FragmentComments method loadYoutubeVideo.

private void loadYoutubeVideo(final String id, final int timeInMillis) {
    if (youTubePlayer != null) {
        toggleYouTubeVisibility(View.VISIBLE);
        if (!id.equals(currentYouTubeId)) {
            youTubePlayer.loadVideo(id);
            currentYouTubeId = id;
        }
        return;
    }
    youTubeFragment = new YouTubePlayerSupportFragment();
    layoutYouTube.setId(youTubeViewId);
    getFragmentManager().beginTransaction().add(youTubeViewId, youTubeFragment, String.valueOf(youTubeViewId)).commit();
    youTubeFragment.initialize(ApiKeys.YOUTUBE_API_KEY, new YouTubePlayer.OnInitializedListener() {

        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean b) {
            FragmentComments.this.youTubePlayer = player;
            youTubePlayer.setManageAudioFocus(false);
            youTubePlayer.setFullscreenControlFlags(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI);
            DisplayMetrics displayMetrics = getActivity().getResources().getDisplayMetrics();
            boolean isLandscape = displayMetrics.widthPixels > displayMetrics.heightPixels;
            if (isLandscape) {
                youTubePlayer.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {

                    @Override
                    public void onFullscreen(boolean fullscreen) {
                        isFullscreen = fullscreen;
                        if (!fullscreen) {
                            releaseYouTube();
                            toggleYouTubeVisibility(View.GONE);
                        }
                    }
                });
            } else {
                youTubePlayer.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {

                    @Override
                    public void onFullscreen(boolean fullscreen) {
                        isFullscreen = fullscreen;
                    }
                });
            }
            youTubePlayer.setPlayerStateChangeListener(new YouTubePlayerStateListener(youTubePlayer, timeInMillis, isLandscape));
            toggleYouTubeVisibility(View.VISIBLE);
            youTubePlayer.loadVideo(id);
            currentYouTubeId = id;
        }

        @Override
        public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
        }
    });
}
Also used : YouTubePlayer(com.google.android.youtube.player.YouTubePlayer) YouTubeInitializationResult(com.google.android.youtube.player.YouTubeInitializationResult) YouTubePlayerSupportFragment(com.google.android.youtube.player.YouTubePlayerSupportFragment) DisplayMetrics(android.util.DisplayMetrics) YouTubePlayerStateListener(com.winsonchiu.reader.utils.YouTubePlayerStateListener)

Aggregations

DisplayMetrics (android.util.DisplayMetrics)1 YouTubeInitializationResult (com.google.android.youtube.player.YouTubeInitializationResult)1 YouTubePlayer (com.google.android.youtube.player.YouTubePlayer)1 YouTubePlayerSupportFragment (com.google.android.youtube.player.YouTubePlayerSupportFragment)1 YouTubePlayerStateListener (com.winsonchiu.reader.utils.YouTubePlayerStateListener)1