2

YouTube player has been released when devices rotate and activity recreated. Then play video. Shows YouTube player has released. I don't know where it released. I have already set it to Null onDestroy Method.

`mPlayer.loadVideo(videoId);`

//pointing YouTube Player released

Joel
  • 14,861
  • 3
  • 27
  • 31
Awais Ali
  • 59
  • 7

3 Answers3

2

My solution is to save the current playing time by YouTubePlayer getCurrentTimeMillis() in onSaveInstanceState(Bundle state) when screen rotation gets triggered, and get the time back from onRestoreInstanceState(Bundle state).

Sample code would be:

static final String CURRENT_PLAY_TIME = "current_play_time";
int current_time;

@Override
protected void onSaveInstanceState(Bundle state) {
    super.onSaveInstanceState(state);
    state.putInt(CURRENT_PLAY_TIME, youTubePlayer.getCurrentTimeMillis());
}

@Override
protected void onRestoreInstanceState(Bundle state) {
    super.onRestoreInstanceState(state);
    current_time = state.getInt(CURRENT_PLAY_TIME);
}

And after restoring the current playing time let YouTubePlayer load video with it.

youTubePlayer.loadVideo(VIDEO_ID, current_time);

Hope it helps!

Max Lee
  • 189
  • 1
  • 4
0

A posible solution is make your app vertical or horizontal permanent

For doing this write within your manifest:

<android:screenOrientation="portrait"> //vertical
<android:screenOrientation="landscape"> //horizontal
Andres Cárdenas
  • 720
  • 1
  • 5
  • 26
  • in one orientation its working perfectly...but need to play after rotate the device either in portrait or landscape...have idea? – Awais Ali Nov 14 '14 at 12:38
  • you can choice landscape or portrait for each activity, but actually you want use the 2 and the problem is when you change between them, may be you need another layout, once for protrait and another for landscape – Andres Cárdenas Nov 14 '14 at 12:40
0
I have found my Excelent solution..all going good with youtube player
@Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);

            if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
                if (mPlayer != null)
                    //Set YouTube Player here;
            }
            if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
                if (mPlayer != null)
                  //Set YouTube Player here;
            }
        }
Awais Ali
  • 59
  • 7