15

My app provides youtube view using Youtube Android SDK. No problem showing videos but it crashes with DeadObjectException when opening youtube app.

My code is like below

//init
FragmentTransaction fragmentTransaction = fm.beginTransaction();

YouTubePlayerSupportFragment fragment = new YouTubePlayerSupportFragment();
fragmentTransaction.replace(R.id.fragmentz, fragment);
fragmentTransaction.commit();

fragment.initialize(Constants.YOUTUBE_DEV_KEY, this);

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, final YouTubePlayer youTubePlayer, boolean wasRestored) {
    if (!wasRestored) {
        this.youTubePlayer = youTubePlayer;
        this.youTubePlayer.setShowFullscreenButton(true);
        try {
            this.youTubePlayer.loadVideo(youtubeLink);
            //other stuff
        } catch (IllegalStateException e) {
            Logger.e(e);
        }
    }
}

and it crashes when 1) move to youtube app directly via intent or 2) move to youtube app via youtube icon on player view.

Crash log is like below

java.lang.IllegalStateException: android.os.DeadObjectException
        at com.google.android.youtube.api.jar.client.RemoteEmbeddedPlayer.u(SourceFile:506)
        at com.google.android.apps.youtube.api.jar.a.a.t(SourceFile:467)
        at com.google.android.youtube.player.internal.h.onTransact(SourceFile:392)
        at android.os.Binder.transact(Binder.java:361)
        at com.google.android.youtube.player.internal.d$a$a.r(Unknown Source)
        at com.google.android.youtube.player.internal.s.h(Unknown Source)
        at com.google.android.youtube.player.YouTubePlayerView.e(Unknown Source)
        at com.google.android.youtube.player.YouTubePlayerSupportFragment.onSaveInstanceState(Unknown Source)
        at android.support.v4.app.Fragment.performSaveInstanceState(Fragment.java:1647)
        at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1610)
        at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1678)
        at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:546)
        at com.actionbarsherlock.app.SherlockFragmentActivity.onSaveInstanceState(SherlockFragmentActivity.java:127)
 Caused by: android.os.DeadObjectException
        at android.os.BinderProxy.transact(Native Method)
        at com.google.android.apps.youtube.api.b.a.aq.k(SourceFile:685)
        at  com.google.android.youtube.api.jar.client.RemoteEmbeddedPlayer.u(SourceFile:503)
        at com.google.android.apps.youtube.api.jar.a.a.t(SourceFile:467)
        at com.google.android.youtube.player.internal.h.onTransact(SourceFile:392)
        at android.os.Binder.transact(Binder.java:361)
        at com.google.android.youtube.player.internal.d$a$a.r(Unknown Source)
        at com.google.android.youtube.player.internal.s.h(Unknown Source)
        at com.google.android.youtube.player.YouTubePlayerView.e(Unknown Source)
        at com.google.android.youtube.player.YouTubePlayerSupportFragment.onSaveInstanceState(Unknown Source)
        at android.support.v4.app.Fragment.performSaveInstanceState(Fragment.java:1647)

please help me to fix this problem.

I've tested on nexus 5, android 4.4, youtube api 1.0, youtube app 5.3.24

kingori
  • 2,406
  • 27
  • 30

1 Answers1

0

This happens when the device is rotating (Or other system config change that cause activity to restart) after youTubeView.initialize and before onInitializationSuccess are called. My understanding is when your rotate your device in this time the original player object will become dead and the thread that suppose to call onInitializationSuccess is still running and once that thread calls onInitializationSuccess it will use the dead player object.

wrap your player with the following try catch will solve the problem:

try{
   youtubeplayer.load(0..;
}catch (IllegalStateException ise){
   //do nothing probably device go rotated
   return;
}

**ANSWER EXTRACTED FROM https://code.google.com/p/gdata-issues/issues/detail?id=4395

Brotoo25
  • 373
  • 3
  • 3
  • well, but that problem occurs even when I don't rotate device. And as the stacktrace says, exception was thrown where I can't catch the exception ex) android.support.v4.app.Fragment.performSaveInstanceState . – kingori Mar 26 '14 at 05:02
  • Well still the problem remains the same. you can have a reference my [Question](http://stackoverflow.com/questions/25747226/youtube-player-crashes-with-giving-java-lang-illegalstateexception-android-os-d). Do you have any other suggestions. – Pravinsingh Waghela Sep 10 '14 at 06:55
  • @PravinsinghWaghela it has nothing to do with null video id, it was your error – user924 Feb 16 '21 at 22:07