Problem : Youtube Player crashes on minimizing youtube player activity/Fragment in the app.
Cause : Youtube Player Update
Code
public class YouTubePlayerActivity extends YouTubeBaseActivity
implements YouTubePlayer.OnInitializedListener {
private static final int RECOVERY_DIALOG_REQUEST = 1;
private final String TAG = YouTubePlayerActivity.class.getSimpleName();
private YouTubePlayerView youTubeView;
private String videoId;
private YouTubePlayer youtubePlayer;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_youtube_player);
videoId = getIntent().getExtras().getString(Constants.BUNDLE_VIDEO_ID);
youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(Constants.YOUTUBE_API_KEY, this);
}
@Override public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
Log.d(TAG, "on Initialization failure : " + errorReason); }
@Override public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
boolean wasRestored) {
youtubePlayer = player;
youtubePlayer.setFullscreen(true);
youtubePlayer.setShowFullscreenButton(false);
youtubePlayer.setFullscreenControlFlags(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION
| YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI);
if (!wasRestored) {
youtubePlayer.loadVideo(videoId);
} }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_DIALOG_REQUEST) {
youTubeView.initialize(Constants.YOUTUBE_API_KEY, this);
} }
@Override protected void onPause() {
super.onPause();
if(youtubePlayer != null) {
youtubePlayer.pause();
} }
@Override protected void onDestroy() {
super.onDestroy();
if (youtubePlayer != null) {
youtubePlayer.release();
} } }