YouTube Android Player API throws “BadParcelableException ClassNotFoundException when unmarshalling: asc” with new YouTube version

自闭症网瘾萝莉.ら 提交于 2019-11-30 09:45:07
xkor

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.

Obviously it's a bug of YouTube app. I'd recommend you to use this hack to erase bundle passed from youtube client to escape this parsing error.

@Override
public void onCreate(Bundle b) {
    if (b != null) {
        b.remove("android:fragments");
    }
    super.onCreate(b);
}

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 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.

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);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!