com.google.android.youtube.player.YouTubePlayer Java Examples

The following examples show how to use com.google.android.youtube.player.YouTubePlayer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: YouTubePlayerController.java    From react-native-youtube with MIT License 6 votes vote down vote up
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
    if (!wasRestored) {
        mYouTubePlayer = youTubePlayer;
        mYouTubePlayer.setPlayerStateChangeListener(this);
        mYouTubePlayer.setPlaybackEventListener(this);
        mYouTubePlayer.setOnFullscreenListener(this);
        updateFullscreen();
        updateShowFullscreenButton();
        updateControls();

        if (mVideoId != null) loadVideo();
        else if (!mVideoIds.isEmpty()) loadVideos();
        else if (mPlaylistId != null) loadPlaylist();
    }
}
 
Example #2
Source File: CustomPlayerControlActivity.java    From YouTubePlayerView-Example with Apache License 2.0 6 votes vote down vote up
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
    if (null == player) return;
    mPlayer = player;

    displayCurrentTime();

    // Start buffering
    if (!wasRestored) {
        player.cueVideo(VIDEO_ID);
    }

    player.setPlayerStyle(PlayerStyle.CHROMELESS);
    mPlayButtonLayout.setVisibility(View.VISIBLE);

    // Add listeners to YouTubePlayer instance
    player.setPlayerStateChangeListener(mPlayerStateChangeListener);
    player.setPlaybackEventListener(mPlaybackEventListener);
}
 
Example #3
Source File: FullscreenDemoActivity.java    From yt-android-player with Apache License 2.0 6 votes vote down vote up
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  int controlFlags = player.getFullscreenControlFlags();
  if (isChecked) {
    // If you use the FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE, your activity's normal UI
    // should never be laid out in landscape mode (since the video will be fullscreen whenever the
    // activity is in landscape orientation). Therefore you should set the activity's requested
    // orientation to portrait. Typically you would do this in your AndroidManifest.xml, we do it
    // programmatically here since this activity demos fullscreen behavior both with and without
    // this flag).
    setRequestedOrientation(PORTRAIT_ORIENTATION);
    controlFlags |= YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE;
  } else {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
    controlFlags &= ~YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE;
  }
  player.setFullscreenControlFlags(controlFlags);
}
 
Example #4
Source File: YoutubeOverlayFragment.java    From AndroidYoutubeOverlay with MIT License 6 votes vote down vote up
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, final YouTubePlayer player, boolean restored) {
    LogHelper.logMessage("onInitializationSuccess: " + restored + " videoId: " + videoId);
    this.player = player;
    player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI);
    player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);
    player.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {

        @Override
        public void onFullscreen(boolean fullscreen) {
            YoutubeOverlayFragment.this.fullscreen = fullscreen;
        }
    });
    if (!restored && videoId != null) {
        player.loadVideo(videoId);
    }
}
 
Example #5
Source File: VideoPlayerFragment.java    From UTubeTV with The Unlicense 6 votes vote down vote up
private void setupFullscreenListener() {
  if (mPlayer == null) {
    DUtils.log("mPlayer is null inside: " + DUtils.currentMethod());
    return;
  }

  mPlayer.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {
    public void onFullscreen(boolean isFullscreen) {
      DUtils.log("setOnFullscreenListener: " + (isFullscreen ? "yes" : "no"));
      VideoPlayerFragment.this.mFullscreen = isFullscreen;

      mFragmentListener.onFullScreen(isFullscreen);
    }

  });
}
 
Example #6
Source File: VideoWallDemoActivity.java    From yt-android-player with Apache License 2.0 5 votes vote down vote up
@Override
public void onInitializationFailure(
    YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
  if (errorReason.isUserRecoverableError()) {
    if (errorDialog == null || !errorDialog.isShowing()) {
      errorDialog = errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST);
      errorDialog.show();
    }
  } else {
    String errorMessage = String.format(getString(R.string.error_player), errorReason.toString());
    Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
  }
}
 
Example #7
Source File: YouTubePlayerController.java    From react-native-youtube with MIT License 5 votes vote down vote up
private void updateControls() {
    switch (mControls) {
        case 0:
            mYouTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
            break;
        case 1:
            mYouTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
            break;
        case 2:
            mYouTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.MINIMAL);
            break;
    }
}
 
Example #8
Source File: CourseUnitYoutubePlayerFragment.java    From edx-app-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onError(YouTubePlayer.ErrorReason errorReason) {
    /*
     * The most common errorReason is because there is a previous player running so this sets free it
     * and reloads the fragment
     */
    if (attempts <= 3) {
        releaseYoutubePlayer();
        initializeHandler.postDelayed(CourseUnitYoutubePlayerFragment.this::initializeYoutubePlayer, 500);
        attempts++;
    } else {
        redirectToYoutubeDialog();
    }
}
 
Example #9
Source File: PlayerControlsDemoActivity.java    From yt-android-player with Apache License 2.0 5 votes vote down vote up
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
    boolean wasRestored) {
  this.player = player;
  player.setPlaylistEventListener(playlistEventListener);
  player.setPlayerStateChangeListener(playerStateChangeListener);
  player.setPlaybackEventListener(playbackEventListener);

  if (!wasRestored) {
    playVideoAtSelection();
  }
  setControlsEnabled(true);
}
 
Example #10
Source File: VideoWallDemoActivity.java    From yt-android-player with Apache License 2.0 5 votes vote down vote up
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
    boolean wasResumed) {
  VideoWallDemoActivity.this.player = player;
  player.setPlayerStyle(PlayerStyle.CHROMELESS);
  player.setPlayerStateChangeListener(new VideoListener());
  maybeStartDemo();
}
 
Example #11
Source File: CourseUnitYoutubePlayerFragment.java    From edx-app-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onInitializationSuccess(Provider provider,
                                    YouTubePlayer player,
                                    boolean wasRestored) {
    if (getActivity() == null) {
        return;
    }
    final int orientation = getActivity().getResources().getConfiguration().orientation;
    int currentPos = 0;
    if (videoModel != null) {
        currentPos = (int) videoModel.getLastPlayedOffset();
    }
    if (!wasRestored) {
        final Uri uri = Uri.parse(unit.getData().encodedVideos.getYoutubeVideoInfo().url);
        /*
         *  Youtube player loads the video using the video id from the url
         *  the url has the following format "https://www.youtube.com/watch?v=3_yD_cEKoCk" where v is the video id
         */
        final String videoId = uri.getQueryParameter("v");
        player.loadVideo(videoId, currentPos);
        youTubePlayer = player;
        youTubePlayer.setPlayerStateChangeListener(new StateChangeListener());
        youTubePlayer.setPlaybackEventListener(new PlaybackListener());
        youTubePlayer.setOnFullscreenListener(new FullscreenListener());
        if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
            youTubePlayer.setFullscreen(true);
        }
    }
}
 
Example #12
Source File: VideoWallDemoActivity.java    From yt-android-player with Apache License 2.0 5 votes vote down vote up
@Override
public void onError(YouTubePlayer.ErrorReason errorReason) {
  if (errorReason == YouTubePlayer.ErrorReason.UNEXPECTED_SERVICE_DISCONNECTION) {
    // player has encountered an unrecoverable error - stop the demo
    flipDelayHandler.removeCallbacksAndMessages(null);
    state = State.UNINITIALIZED;
    thumbnailLoader.release();
    thumbnailLoader = null;
    player = null;
  } else {
    state = State.VIDEO_ENDED;
  }
}
 
Example #13
Source File: YouTubeFailureRecoveryActivity.java    From yt-android-player with Apache License 2.0 5 votes vote down vote up
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
    YouTubeInitializationResult errorReason) {
  if (errorReason.isUserRecoverableError()) {
    errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
  } else {
    String errorMessage = String.format(getString(R.string.error_player), errorReason.toString());
    Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
  }
}
 
Example #14
Source File: PlayerViewDemoActivity.java    From yt-android-player with Apache License 2.0 5 votes vote down vote up
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
    boolean wasRestored) {
  if (!wasRestored) {
    player.cueVideo("wKJ9KzGQq0w");
  }
}
 
Example #15
Source File: FullscreenDemoActivity.java    From yt-android-player with Apache License 2.0 5 votes vote down vote up
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
    boolean wasRestored) {
  this.player = player;
  setControlsEnabled();
  // Specify that we want to handle fullscreen behavior ourselves.
  player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);
  player.setOnFullscreenListener(this);
  if (!wasRestored) {
    player.cueVideo("avP5d16wEp0");
  }
}
 
Example #16
Source File: FragmentDemoActivity.java    From yt-android-player with Apache License 2.0 5 votes vote down vote up
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
    boolean wasRestored) {
  if (!wasRestored) {
    player.cueVideo("nCgQDjiotG0");
  }
}
 
Example #17
Source File: ActionBarDemoActivity.java    From yt-android-player with Apache License 2.0 5 votes vote down vote up
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
    boolean wasRestored) {
  player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);
  player.setOnFullscreenListener(this);

  if (!wasRestored) {
    player.cueVideo("9c6W4CCU9M4");
  }
}
 
Example #18
Source File: VideoListDemoActivity.java    From yt-android-player with Apache License 2.0 5 votes vote down vote up
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean restored) {
  this.player = player;
  player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);
  player.setOnFullscreenListener((VideoListDemoActivity) getActivity());
  if (!restored && videoId != null) {
    player.cueVideo(videoId);
  }
}
 
Example #19
Source File: YouTubePlayerController.java    From react-native-youtube with MIT License 5 votes vote down vote up
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult result) {
    if (result.isUserRecoverableError()) {
        result.getErrorDialog(mYouTubeView.getReactContext().getCurrentActivity(), 0).show();
    }
    mYouTubeView.receivedError(result.toString());
}
 
Example #20
Source File: YouTubeFragment.java    From android-inline-youtube-view with Apache License 2.0 5 votes vote down vote up
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult result) {
    youTubePlayer = null;
    if (listener != null) {
        listener.onInitializationFailure(result.name());
    }
}
 
Example #21
Source File: PlayerActivity.java    From search-youtube with Apache License 2.0 5 votes vote down vote up
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,
                                    boolean restored) {
    
    //initialise the video player only if it is not restored or is not yet set
    if(!restored){

        //cueVideo takes video ID as argument and initialise the player with that video
        //this method just prepares the player to play the video
        //but does not download any of the video stream until play() is called
        player.cueVideo(getIntent().getStringExtra("VIDEO_ID"));
    }
}
 
Example #22
Source File: Fragment2.java    From Stayfit with Apache License 2.0 5 votes vote down vote up
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
    if (youTubeInitializationResult.isUserRecoverableError()) {
        youTubeInitializationResult.getErrorDialog(getActivity(), RECOVERY_DIALOG_REQUEST).show();
    } else {
        //Handle the failure
        Toast.makeText(getActivity(), "onInitializationFailure", Toast.LENGTH_LONG).show();
    }
}
 
Example #23
Source File: Fragment1.java    From Stayfit with Apache License 2.0 5 votes vote down vote up
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
    if (youTubeInitializationResult.isUserRecoverableError()) {
        youTubeInitializationResult.getErrorDialog(getActivity(), RECOVERY_DIALOG_REQUEST).show();
    } else {
        //Handle the failure
        Toast.makeText(getActivity(), "onInitializationFailure", Toast.LENGTH_LONG).show();
    }
}
 
Example #24
Source File: Fragment3.java    From Stayfit with Apache License 2.0 5 votes vote down vote up
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
    if (youTubeInitializationResult.isUserRecoverableError()) {
        youTubeInitializationResult.getErrorDialog(getActivity(), RECOVERY_DIALOG_REQUEST).show();
    } else {
        //Handle the failure
        Toast.makeText(getActivity(), "onInitializationFailure", Toast.LENGTH_LONG).show();
    }
}
 
Example #25
Source File: ContentFragment.java    From newsApp with Apache License 2.0 5 votes vote down vote up
private void startYoutubeVideo() {
    Intent intent = new Intent(getActivity(), YouTubePlayerActivity.class);
    String vidoeId = YouTubeUrlParser.getVideoId(youtubeUrl);
// Youtube video ID (Required, You can use YouTubeUrlParser to parse Video Id from url)
    intent.putExtra(YouTubePlayerActivity.EXTRA_VIDEO_ID, vidoeId);

// Youtube player style (DEFAULT as default)
    intent.putExtra(YouTubePlayerActivity.EXTRA_PLAYER_STYLE, YouTubePlayer.PlayerStyle.DEFAULT);

// Screen Orientation Setting (AUTO for default)
// AUTO, AUTO_START_WITH_LANDSCAPE, ONLY_LANDSCAPE, ONLY_PORTRAIT
    intent.putExtra(YouTubePlayerActivity.EXTRA_ORIENTATION, Orientation.AUTO);

// Show audio interface when user adjust volume (true for default)
    intent.putExtra(YouTubePlayerActivity.EXTRA_SHOW_AUDIO_UI, true);

// If the video is not playable, use Youtube app or Internet Browser to play it
// (true for default)
    intent.putExtra(YouTubePlayerActivity.EXTRA_HANDLE_ERROR, true);

// Animation when closing youtubeplayeractivity (none for default)
    intent.putExtra(YouTubePlayerActivity.EXTRA_ANIM_ENTER, R.anim.fade_in);
    intent.putExtra(YouTubePlayerActivity.EXTRA_ANIM_EXIT, R.anim.fade_out);

    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
  }
 
Example #26
Source File: FragmentDemoActivity.java    From yt-android-player with Apache License 2.0 4 votes vote down vote up
@Override
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
  return (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtube_fragment);
}
 
Example #27
Source File: YouTubeFragment.java    From android-inline-youtube-view with Apache License 2.0 4 votes vote down vote up
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, final YouTubePlayer player, boolean restored) {
    youTubePlayer = player;
    youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
    youTubePlayer.setShowFullscreenButton(false);

    if (listener != null) {
        listener.onReady();
    }

    youTubePlayer.setPlaybackEventListener(new YouTubePlayer.PlaybackEventListener() {
        @Override
        public void onPlaying() {
            if (listener != null && youTubePlayer != null && !PlayerStateList.PLAYING.equals(playerState)) {
                playerState = PlayerStateList.PLAYING;
                listener.onPlay(youTubePlayer.getCurrentTimeMillis());
            }
        }

        @Override
        public void onPaused() {
            handleOnPauseEvent();
        }

        @Override
        public void onStopped() {
            //since these are player stop events in case of any player error so pause event not stop.
            handleOnPauseEvent();
        }

        @Override
        public void onBuffering(boolean isBuffering) {
            if (listener != null && youTubePlayer != null) {
                listener.onBuffering((youTubePlayer.getCurrentTimeMillis()), isBuffering);
            }
        }

        @Override
        public void onSeekTo(int newPositionMillis) {
            //just to mimick the pause event before play on seek event.
            handleOnPauseEvent();
        }
    });

    youTubePlayer.setPlayerStateChangeListener(new YouTubePlayer.PlayerStateChangeListener() {
        @Override
        public void onLoading() {
            //intentionally left blank
        }

        @Override
        public void onLoaded(String s) {
            //intentionally left blank
        }

        @Override
        public void onAdStarted() {
            //intentionally left blank
        }

        @Override
        public void onVideoStarted() {
            //intentionally left blank
        }

        @Override
        public void onVideoEnded() {
            handleStopEvent();
        }

        @Override
        public void onError(YouTubePlayer.ErrorReason errorReason) {
            //intentionally left blank
        }
    });

    if (!restored) {
        //do any work here to cue video, play video, etc.
        Bundle arguments = getArguments();
        String videoId = arguments != null ? arguments.getString(ARG_VIDEO_ID) : null;
        if (!TextUtils.isEmpty(videoId)) {
            youTubePlayer.loadVideo(videoId);
        }
    }
}
 
Example #28
Source File: YouTubePlayerController.java    From react-native-youtube with MIT License 4 votes vote down vote up
@Override
public void onError(YouTubePlayer.ErrorReason errorReason) {
    mYouTubeView.receivedError(errorReason.toString());
}
 
Example #29
Source File: FullscreenDemoActivity.java    From yt-android-player with Apache License 2.0 4 votes vote down vote up
@Override
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
  return playerView;
}
 
Example #30
Source File: YouTubeActivity.java    From android-inline-youtube-view with Apache License 2.0 4 votes vote down vote up
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, final YouTubePlayer player, boolean restored) {
    youTubePlayer = player;
    youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
    youTubePlayer.setShowFullscreenButton(true);
    youTubePlayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION);
    youTubePlayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI);

    youTubePlayer.setPlaybackEventListener(new YouTubePlayer.PlaybackEventListener() {
        @Override
        public void onPlaying() {
            if (youTubePlayer != null && !PlayerStateList.PLAYING.equals(playerState)) {
                playerState = PlayerStateList.PLAYING;
            }
        }

        @Override
        public void onPaused() {
            handleOnPauseEvent();
        }

        @Override
        public void onStopped() {
            handleStopEvent();
        }

        @Override
        public void onBuffering(boolean isBuffering) {
            //intentionally left blank
        }

        @Override
        public void onSeekTo(int newPositionMillis) {
            handleOnPauseEvent();
        }
    });

    youTubePlayer.setPlayerStateChangeListener(new YouTubePlayer.PlayerStateChangeListener() {
        @Override
        public void onLoading() {
            //intentionally left blank
        }

        @Override
        public void onLoaded(String s) {
            //intentionally left blank
        }

        @Override
        public void onAdStarted() {
            //intentionally left blank
        }

        @Override
        public void onVideoStarted() {
            //intentionally left blank
        }

        @Override
        public void onVideoEnded() {
            handleStopEvent();
        }

        @Override
        public void onError(YouTubePlayer.ErrorReason errorReason) {
            //intentionally left blank
        }
    });

    player.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {
        @Override
        public void onFullscreen(boolean b) {
            handleStopEvent();
        }
    });

    if (!restored) {
        youTubePlayer.loadVideo(getVideoId());
    }
}