Filing bug to YouTube Android Player API Library engineers: see android-youtube-api tag
Over the course of the past week and a half, I\'ve noticed t
Use following code in your onCreate:
@Override
public void onCreate(Bundle b) {
if (b != null) {
final ClassLoader contextCl = getClassLoader();
b.setClassLoader(contextCl);
}
super.onCreate(b);
}
The bug is probably due to YouTube library code storing custom Parcelable inside the state Bundle (YouTube library classes are probably obfuscated by Proguard, hence weird names like "asc").
If the suggestion above does not work, follow along the lines of @Fitz's suggestion and drop the state Bundle. Don't try to mess with it in onCreate (that won't work), instead it is best to override onSaveInstanceState to return Bundle.EMPTY if your app is suspended when you have a playback in progress.
Note, that the bug in question is pretty tricky in multiple ways. If you (or YouTube app) store nested Bundles inside each other, those also need to have proper ClassLoader set… Someone should tell Google to get some common sense and just use Thread#getContextClassLoader inside Bundle to fix that problem once and for all.
I just ended up using a try catch block that worked for me.
try {
super.onCreate(savedInstanceState);
} catch (BadParcelableException e) {
// Maybe put an analytic here to see if it is still being thrown.
// So when youtube fixes the issue in the future we can remove this
// ugly try catch.
Log.e(TAG, e.toString);
}
The problem in view state saving and restoring. Youtube app have a bug and store view state which contains instance of class asc
from youtube apk. So our app can not restore it because known nothing about this class. My solution is preventing view state saving for YoutubePlayerView
in YouTubePlayerSupportFragment
by next code:
@Override
public void onSaveInstanceState(Bundle bundle) {
super.onSaveInstanceState(bundle);
// disable view state saving to prevent saving states from youtube apk which cannot be restored.
View view = getView();
if (view instanceof ViewGroup) {
ViewGroup viewGroup = ((ViewGroup) view);
for (int i = 0; i < viewGroup.getChildCount(); i++) {
viewGroup.getChildAt(i).setSaveFromParentEnabled(false);
}
}
}
This code should be added to your subclass of YouTubePlayerSupportFragment
.
This solution does not remove youtube player state from bundle. So youtube player will be restored successfully.
I found a solution that works for me. In my onCreate
method I catch the RuntimeException
that gets thrown due to the ClassNotFoundException
in the youtube player. The video loses its state. Pressing play causes it to start over. Otherwise business as usual.