从Android youtube播放器api获取大量崩溃

时间:2016-04-05 07:12:42

标签: android youtube youtube-api android-youtube-api

我正在使用android's youtube player api版本1.2.1(尝试使用最新版本1.2.2)。它在大多数设备上都能正常工作。然而,偶尔,我会继续崩溃崩溃。我得到以下崩溃

Fatal Exception: java.lang.IllegalStateException: android.os.TransactionTooLargeException
   at com.google.android.youtube.api.jar.client.RemoteEmbeddedPlayer.x(SourceFile:558)
   at bpd.w(SourceFile:576)
   at tef.onTransact(SourceFile:390)
   at android.os.Binder.transact(Binder.java:395)
   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:1936)
   at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1654)
   at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1722)
   at android.support.v4.app.Fragment.performSaveInstanceState(Fragment.java:1938)
   at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1654)
   at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1722)
   at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:527)
   at com.newshunt.news.activities.NewsBaseActivity.onSaveInstanceState(NewsBaseActivity.java:56)
   at com.newshunt.news.activities.NewsDetailsActivity.onSaveInstanceState(NewsDetailsActivity.java:613)
   at android.app.Activity.performSaveInstanceState(Activity.java:1388)
   at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1286)
   at android.app.ActivityThread.callCallActivityOnSaveInstanceState(ActivityThread.java:4588)
   at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3960)
   at android.app.ActivityThread.handleStopActivity(ActivityThread.java:4023)
   at android.app.ActivityThread.access$1200(ActivityThread.java:181)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1498)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:145)
   at android.app.ActivityThread.main(ActivityThread.java:6117)
   at java.lang.reflect.Method.invoke(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

还会因以下异常而崩溃。

Fatal Exception: java.lang.IllegalStateException: android.os.DeadObjectException
   at com.google.android.apps.youtube.api.jar.a.eo.surfaceDestroyed(SourceFile:236)
   at android.view.SurfaceView.updateWindow(SurfaceView.java:589)
   at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:237)
   at android.view.View.dispatchDetachedFromWindow(View.java:12854)
   at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2757)
   at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2757)
   at android.view.ViewGroup.removeViewInternal(ViewGroup.java:3844)
   at android.view.ViewGroup.removeViewInternal(ViewGroup.java:3819)
   at android.view.ViewGroup.removeView(ViewGroup.java:3751)
   at com.google.android.youtube.player.YouTubePlayerView$1.b(Unknown Source)
   at com.google.android.youtube.player.internal.r.h(Unknown Source)
   at com.google.android.youtube.player.internal.r$e.onServiceDisconnected(Unknown Source)
   at android.app.LoadedApk$ServiceDispatcher.doDeath(LoadedApk.java:1111)
   at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1125)
   at android.os.Handler.handleCallback(Handler.java:725)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:176)
   at android.app.ActivityThread.main(ActivityThread.java:5317)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
   at dalvik.system.NativeStart.main(NativeStart.java)

崩溃恰好出现在像5.2.27这样的youtube应用程序的特定版本中。虽然针对这些崩溃提出了很多问题,但youtube开发人员没有回答如何缓解此问题。

一些开发人员提出了以下解决方法

1)使用loadVideo代替cueVideo。但我无法使用此解决方法,因为loadVideo始终自动播放视频,而这不是我的应用程序的要求。还有人提到loadVideo同样,虽然在某些不同的版本中,但这个问题仍在发生。

2)检查代码以检查youtube应用程序版本,然后输入特定代码。现在这种方法的问题是我必须检查所发布的youtube app的每个版本,并检查导致问题的版本,这不是一个好的解决方法。

现在是否有任何我可以应用的修复程序来避免此问题,或者是youtube开发人员计划发布一些内部处理所有这些问题的jar?

2 个答案:

答案 0 :(得分:0)

我通过在try catch块中放置youtube调用(如youtubePlayer.loadVideo(),cueVideo(),getCurrentTimeMillis()等)来减少错误发生,并捕获IllegalStateException异常,然后重新初始化youtube播放器。

要创建YoutubePlayer的新实例,只需在catch块中调用initialize()方法。

示例:

 if (youtubePlayer != null) {
        try {
            youtubePlayer.loadVideo(videoId);
        } catch (IllegalStateException e) {
            initialize(API_KEY, this);
        }
    }

但是bug仍然存在,我通过捕获这些异常并重新启动活动来解决它。这个未被捕获的异常并且为了捕获它们,您需要使用UncaughtExceptionHandler

示例:

 private Thread.UncaughtExceptionHandler defaultUEH;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    defaultUEH = Thread.getDefaultUncaughtExceptionHandler();

    // setup handler for uncaught exception
    Thread.setDefaultUncaughtExceptionHandler(_unCaughtExceptionHandler);
}

   private Thread.UncaughtExceptionHandler _unCaughtExceptionHandler =
                new Thread.UncaughtExceptionHandler() {
                    @Override
                    public void uncaughtException(Thread thread, Throwable ex) {
                        Log.e(TAG, "uncaughtException: ", ex);
                        PendingIntent myActivity = PendingIntent.getActivity(getApplicationContext(),
                                192837, new Intent(getApplicationContext(), MainActivity.class),
                                PendingIntent.FLAG_ONE_SHOT);

                        AlarmManager alarmManager;
                        alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
                        alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                                15000, myActivity );
                        System.exit(2);

                        // re-throw critical exception further to the os (important)
                        defaultUEH.uncaughtException(thread, ex);
                    }
                };

答案 1 :(得分:0)

Android YouTube Player API不稳定,其中有known bugs。来自YouTube的团队表示,他们将发布新版本的图书馆。

目前,我找到的最佳解决方案是构建my own library