This is how I managed to get rid of this exception. In my case it was thrown on onSaveInstanceState where I tried to save current player position using the same piece of code:
if(youtubePlayer != null){ time = youtubePlayer.getCurrentTimeMillis(); }
and upon successfull player initialization in onInitializationSuccess I continued playing video using the time value assigned in onCreate from Bundle.
But it turned out that such approach is unnecessary. To avoid the exception throw I just added android:configChanges="orientation|screenSize"
attribute to my player activity in manifest. This makes the system handle orientation changes by itself, and adding time parameter to cueing the video becomes redundant. This is what checking the wasRestored flag is made for in onInitializationSuccess.
Here's the changes summary:
AndroidManifest.xml:
<activity android:name=".VideoPlayerActivity"
android:configChanges="orientation|screenSize"/>
VideoPlayerActivity.java:
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
this.mPlayer = player;
if (!wasRestored) {
mPlayer.loadVideo(mVideoId);
}
}