user5035668
user5035668

Reputation: 307

Android: When playing an youtube video in full screen activity is realoaded

I have an you tube player inside my activity. But I am experiencing some issues with the full screen option of the video.

When I first start the activity the youtubeview is shown and initialized correctly I can see the video play it and go to full screen with it. But when I press the button to go out of full screen mode/rotate my phone the whole activity is being reloaded and the youtubeview is missing from the activity. Here is how I initialize the youtube player/view:

   try {
        final YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);

        youTubeView.initialize("KEY", new YouTubePlayer.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean VidBool) {
                if(ad==null || ad.getVideo_urls() == null)
                    return;

                if (!VidBool)
                {
                    try {
                        if (ad.getVideo_urls() != null && ad.getVideo_urls().length() > 0) {
                            String url = ad.getVideo_urls().getString(0);
                            if (url.contains("youtube")){                                    
                                VideoID = "kQsN-pvokrw";
                                youTubeView.setVisibility(View.VISIBLE);
                                MyYouTubePlayer = youTubePlayer;
                                MyYouTubePlayer.cueVideo(VideoID);
                                MyYouTubePlayer.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {
                                    @Override
                                    public void onFullscreen(boolean b) {
                                        fullScreen = true;
                                    }
                                });
                            }
                        } else {
                            youTubeView.setVisibility(View.GONE);
                            Log.i(Constants.getTag(), "Video not found");
                            //Making sure the MyYouTubePlayer is null and if not is being released
                            if(MyYouTubePlayer != null)
                            {
                                MyYouTubePlayer.release();
                                MyYouTubePlayer.setFullscreenControlFlags(-1);
                            }
                        }
                    }
                    catch (JSONException e) {
                        youTubePlayer.release();
                        e.printStackTrace();
                    }
                }

            }

            @Override
            public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
                youTubeView.removeAllViews();
            }
        });



        //Catching A dead object exception
    } catch(Exception e){
        e.printStackTrace();
        Log.e("Youtube", "error initializing youtube");
    }

Upvotes: 0

Views: 1391

Answers (1)

rolgalan
rolgalan

Reputation: 1661

If you don't want your Activty to be recreated after an orientation change, you have to add next line to your Manifest (inside the activity tag):

android:configChanges="orientation|screenSize"

But then you should override the onConfigurationChanged method of that Activity if you want to support orientation changes.

@Override
public void onConfigurationChanged(Configuration newConfig) {}

Further reading: http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange

Upvotes: 2

Related Questions