从安卓youtube播放器api获得很多崩溃



我使用的是android's youtube player api的1.2.1版本(已试用最新版本1.2.2)。它在大多数设备上运行良好。然而,时不时地,我在crashlytics上不断崩溃。我得到以下崩溃

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)

崩溃恰好出现在特定版本的youtube应用程序中,如5.2.27。尽管这些崩溃有很多问题,但youtube开发人员尚未就如何缓解这一问题做出回复。

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

1) 使用loadVideo而不是cueVideo。但我不能使用这种解决方法,因为loadVideo总是自动播放视频,这不是我的应用程序的要求。还有人提到,对于loadVideo,这个问题也在发生,尽管在一些不同的版本中。

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

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

我通过在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);
        }
    }

但是错误仍然存在,我通过捕捉这些异常并重新启动活动来解决它。这个未捕获的异常,要捕获它们,您需要使用UncaughtException处理程序

示例:

 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);
                    }
                };

android YouTube Player API不稳定,其中存在已知的错误。YouTube团队表示,他们将发布该库的新版本。

目前,我找到的最好的解决方案是建立自己的库。

相关内容

  • 没有找到相关文章