使用YouTube Data API时崩溃

时间:2014-09-29 15:38:37

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

我的应用程序使用YouTube Data API v3播放YouTube视频。我有一个使用VideoPlayerActivity播放Youtube视频的活动YouTubeSupportFragment。在播放第一个视频时,我还允许用户选择要播放的其他视频,它会自己启动VideoPlayerActivity来播放第二个视频。一切顺利。但是,当我按“返回”再次观看第一个视频时,按下YoutubePlayer控制中的“播放”按钮,它会崩溃。
为简单起见,我使用FragmentDemoActivity示例中的YouTubeAndroidPlayerApi。在此活动中,我有一个按钮,当用户点击它时,这将开始FragmentDemoActivity自己播放第二个视频。一切正常。但是当按下Back并单击YoutubePlayer controll上的播放按钮时。它崩溃了。 这是代码。

public class FragmentDemoActivity extends YouTubeFailureRecoveryActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("FragmentDemoActivity", "onCreate");
        setContentView(R.layout.fragments_demo);

        YouTubePlayerFragment youTubePlayerFragment = (YouTubePlayerFragment) getFragmentManager()
                .findFragmentById(R.id.youtube_fragment);
        youTubePlayerFragment.initialize(DeveloperKey.DEVELOPER_KEY, this);

        (findViewById(R.id.btn_click))
                .setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent intent = new Intent(FragmentDemoActivity.this,
                                FragmentDemoActivity.class);
                        startActivity(intent);
                    }
                });
    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider,
            YouTubePlayer player, boolean wasRestored) {
        Log.e("FragmentDemoActivity", "onInitializationSuccess " + wasRestored);
        if (!wasRestored) {
            player.cueVideo("VvseoF9eASw");
        }
    }

    @Override
    protected YouTubePlayer.Provider getYouTubePlayerProvider() {
        return (YouTubePlayerFragment) getFragmentManager().findFragmentById(
                R.id.youtube_fragment);
    }

}

这是logcat

09-29 22:18:31.181: E/AndroidRuntime(26398): FATAL EXCEPTION: main
09-29 22:18:31.181: E/AndroidRuntime(26398): java.lang.RuntimeException: PlaybackMonitor queried outside playback sequence
09-29 22:18:31.181: E/AndroidRuntime(26398):    at ckj.j(PG:114)
09-29 22:18:31.181: E/AndroidRuntime(26398):    at ckj.i(PG:101)
09-29 22:18:31.181: E/AndroidRuntime(26398):    at cjt.N(PG:746)
09-29 22:18:31.181: E/AndroidRuntime(26398):    at cjt.e(PG:673)
09-29 22:18:31.181: E/AndroidRuntime(26398):    at ckl.i(PG:252)
09-29 22:18:31.181: E/AndroidRuntime(26398):    at cps.a(PG:295)
09-29 22:18:31.181: E/AndroidRuntime(26398):    at abq.run(PG:356)
09-29 22:18:31.181: E/AndroidRuntime(26398):    at android.os.Handler.handleCallback(Handler.java:730)
09-29 22:18:31.181: E/AndroidRuntime(26398):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-29 22:18:31.181: E/AndroidRuntime(26398):    at android.os.Looper.loop(Looper.java:150)
09-29 22:18:31.181: E/AndroidRuntime(26398):    at android.app.ActivityThread.main(ActivityThread.java:5390)
09-29 22:18:31.181: E/AndroidRuntime(26398):    at java.lang.reflect.Method.invokeNative(Native Method)
09-29 22:18:31.181: E/AndroidRuntime(26398):    at java.lang.reflect.Method.invoke(Method.java:525)
09-29 22:18:31.181: E/AndroidRuntime(26398):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-29 22:18:31.181: E/AndroidRuntime(26398):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-29 22:18:31.181: E/AndroidRuntime(26398):    at dalvik.system.NativeStart.main(Native Method)
09-29 22:18:31.181: E/AndroidRuntime(26398): Caused by: java.lang.Throwable: currentPlaybackSequenceMonitor became null here
09-29 22:18:31.181: E/AndroidRuntime(26398):    at ckj.b(PG:61)
09-29 22:18:31.181: E/AndroidRuntime(26398):    at cvc.j(PG:171)
09-29 22:18:31.181: E/AndroidRuntime(26398):    at ckl.x(PG:178)
09-29 22:18:31.181: E/AndroidRuntime(26398):    at ckl.a(PG:191)
09-29 22:18:31.181: E/AndroidRuntime(26398):    at ckl.b(PG:733)
09-29 22:18:31.181: E/AndroidRuntime(26398):    at pq.d(PG:510)
09-29 22:18:31.181: E/AndroidRuntime(26398):    at zj.run(PG:408)
09-29 22:18:31.181: E/AndroidRuntime(26398):    ... 9 more
09-29 22:18:31.251: E/IMGSRV(167): :0: PVRDRMOpen: TP3, ret = 42
09-29 22:18:31.311: E/IMGSRV(167): :0: PVRDRMOpen: TP3, ret = 43
09-29 22:18:32.221: E/IMGSRV(167): :0: PVRDRMOpen: TP3, ret = 34
09-29 22:18:32.441: E/IMGSRV(167): :0: PVRDRMOpen: TP3, ret = 43
09-29 22:18:33.141: E/IMGSRV(167): :0: PVRDRMOpen: TP3, ret = 76
09-29 22:18:33.211: E/AndroidRuntime(26229): FATAL EXCEPTION: main
09-29 22:18:33.211: E/AndroidRuntime(26229): java.lang.RuntimeException: Unable to pause activity {com.examples.youtubeapidemo/com.examples.youtubeapidemo.FragmentDemoActivity}: java.lang.IllegalStateException: android.os.DeadObjectException
09-29 22:18:33.211: E/AndroidRuntime(26229):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3220)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3175)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3153)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at android.app.ActivityThread.access$800(ActivityThread.java:148)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at android.os.Looper.loop(Looper.java:150)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at android.app.ActivityThread.main(ActivityThread.java:5390)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at java.lang.reflect.Method.invokeNative(Native Method)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at java.lang.reflect.Method.invoke(Method.java:525)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at dalvik.system.NativeStart.main(Native Method)
09-29 22:18:33.211: E/AndroidRuntime(26229): Caused by: java.lang.IllegalStateException: android.os.DeadObjectException
09-29 22:18:33.211: E/AndroidRuntime(26229):    at com.google.android.youtube.api.jar.client.RemoteEmbeddedPlayer.i(PG:522)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at sc.g(PG:174)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at fkz.onTransact(PG:360)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at android.os.Binder.transact(Binder.java:347)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at com.google.android.youtube.player.internal.d$a$a.o(Unknown Source)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at com.google.android.youtube.player.internal.s.d(Unknown Source)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at com.google.android.youtube.player.YouTubePlayerView.c(Unknown Source)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at com.google.android.youtube.player.YouTubePlayerFragment.onPause(Unknown Source)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at android.app.Fragment.performPause(Fragment.java:1854)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:930)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1039)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at android.app.FragmentManagerImpl.dispatchPause(FragmentManager.java:1861)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at android.app.Activity.performPause(Activity.java:5403)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1233)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3206)
09-29 22:18:33.211: E/AndroidRuntime(26229):    ... 12 more
09-29 22:18:33.211: E/AndroidRuntime(26229): Caused by: android.os.DeadObjectException
09-29 22:18:33.211: E/AndroidRuntime(26229):    at android.os.BinderProxy.transact(Native Method)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at aai.e(PG:629)
09-29 22:18:33.211: E/AndroidRuntime(26229):    at com.google.android.youtube.api.jar.client.RemoteEmbeddedPlayer.i(PG:519)

09-29 22:21:02.441: E/ActivityThread(26565): Activity com.examples.youtubeapidemo.FragmentDemoActivity has leaked ServiceConnection com.google.android.youtube.player.internal.r$e@21d28598 that was originally bound here
09-29 22:21:02.441: E/ActivityThread(26565): android.app.ServiceConnectionLeaked: Activity com.examples.youtubeapidemo.FragmentDemoActivity has leaked ServiceConnection com.google.android.youtube.player.internal.r$e@21d28598 that was originally bound here
09-29 22:21:02.441: E/ActivityThread(26565):    at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:974)
09-29 22:21:02.441: E/ActivityThread(26565):    at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:868)
09-29 22:21:02.441: E/ActivityThread(26565):    at android.app.ContextImpl.bindServiceAsUser(ContextImpl.java:1525)
09-29 22:21:02.441: E/ActivityThread(26565):    at android.app.ContextImpl.bindService(ContextImpl.java:1513)
09-29 22:21:02.441: E/ActivityThread(26565):    at android.content.ContextWrapper.bindService(ContextWrapper.java:497)
09-29 22:21:02.441: E/ActivityThread(26565):    at com.google.android.youtube.player.internal.r.e(Unknown Source)
09-29 22:21:02.441: E/ActivityThread(26565):    at com.google.android.youtube.player.YouTubePlayerView.a(Unknown Source)
09-29 22:21:02.441: E/ActivityThread(26565):    at com.google.android.youtube.player.YouTubePlayerFragment.a(Unknown Source)
09-29 22:21:02.441: E/ActivityThread(26565):    at com.google.android.youtube.player.YouTubePlayerFragment.initialize(Unknown Source)
09-29 22:21:02.441: E/ActivityThread(26565):    at com.examples.youtubeapidemo.FragmentDemoActivity.onCreate(FragmentDemoActivity.java:49)
09-29 22:21:02.441: E/ActivityThread(26565):    at android.app.Activity.performCreate(Activity.java:5303)
09-29 22:21:02.441: E/ActivityThread(26565):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-29 22:21:02.441: E/ActivityThread(26565):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2296)
09-29 22:21:02.441: E/ActivityThread(26565):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
09-29 22:21:02.441: E/ActivityThread(26565):    at android.app.ActivityThread.access$600(ActivityThread.java:148)
09-29 22:21:02.441: E/ActivityThread(26565):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
09-29 22:21:02.441: E/ActivityThread(26565):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-29 22:21:02.441: E/ActivityThread(26565):    at android.os.Looper.loop(Looper.java:150)
09-29 22:21:02.441: E/ActivityThread(26565):    at android.app.ActivityThread.main(ActivityThread.java:5390)
09-29 22:21:02.441: E/ActivityThread(26565):    at java.lang.reflect.Method.invokeNative(Native Method)
09-29 22:21:02.441: E/ActivityThread(26565):    at java.lang.reflect.Method.invoke(Method.java:525)
09-29 22:21:02.441: E/ActivityThread(26565):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-29 22:21:02.441: E/ActivityThread(26565):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-29 22:21:02.441: E/ActivityThread(26565):    at dalvik.system.NativeStart.main(Native Method)
09-29 22:21:04.541: E/FragmentDemoActivity(26565): onCreate
09-29 22:21:04.821: E/FragmentDemoActivity(26565): onInitializationSuccess false

任何帮助将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:1)

就这样做

创建

private YouTubePlayer youTubePlayer;

然后初始化youTubePlayer = player;

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
    Log.e("FragmentDemoActivity", "onInitializationSuccess " + wasRestored);
    if (!wasRestored) {
        player.cueVideo("VvseoF9eASw");
    }
    youTubePlayer = player;
}

并覆盖onResume()

@Override
protected void onResume() {
    super.onResume();
    if (youTubePlayer !=null) {
        youTubePlayer.cueVideo("VvseoF9eASw");
}

P.S。

如果您想从上一刻开始播放视频(暂停),请在VideoPlayerActivity mPlayer.getCurrentTimeMillis()中获取暂停时间并发送至FragmentDemoActivity

之后将onResume()中的FragmentDemoActivity更改为

@Override
protected void onResume() {
    super.onResume();
    if (youTubePlayer !=null) {
        youTubePlayer.cueVideo("VvseoF9eASw", time_from_video_player_activity);
}