YouTubePlayerFragment 后台堆栈

YouTubePlayerFragment backstack(YouTubePlayerFragment 后台堆栈)

本文介绍了YouTubePlayerFragment 后台堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的活动中使用 YouTubePlayerFragment 和 backstack.因此,当我按下后退按钮片段消失并且将来的某些操作(例如按钮单击)时再次出现另一个视频.这是我的代码

I want to use YouTubePlayerFragment in my activity with backstack. So when I press back button fragment disappears and on some action in future (button click for example) appears again with another video. Here's my code

public class YouTubeTestActivity extends Activity implements YouTubePlayer.OnInitializedListener {

@OnClick(R.id.button)
public void click(){
    init();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);
    ButterKnife.inject(this);

    init();

}

private void init() {

    YouTubePlayerFragment f = YouTubePlayerFragment.newInstance();
    f.initialize(DeveloperKey.KEY, this);
    getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.frame,f).commit();
}

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
   youTubePlayer.loadVideo("O4sXdktFDEM");
}

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
    Toasts.fromText(this, "ERROR");
}

}

当我在该片段初始化后回击并单击按钮时,我看到拇指,但视频不播放,只有无限的旋转进度和 logcat 中的混淆错误:

When i hit back and click button after that fragment initialized, i see thumb, but video doesn't play, only infinite spinning progress and obfuscated errors in logcat:

    11-04 16:30:52.597 8530-8530/? W/MessageQueue: Handler (android.os.Handler) {d856ff1} sending message to a Handler on a dead thread
11-04 16:30:52.597 8530-8530/? W/MessageQueue: java.lang.IllegalStateException: Handler (android.os.Handler) {d856ff1} sending message to a Handler on a dead thread
11-04 16:30:52.597 8530-8530/? W/MessageQueue:     at android.os.MessageQueue.enqueueMessage(MessageQueue.java:543)
11-04 16:30:52.597 8530-8530/? W/MessageQueue:     at android.os.Handler.enqueueMessage(Handler.java:631)
11-04 16:30:52.597 8530-8530/? W/MessageQueue:     at android.os.Handler.sendMessageAtTime(Handler.java:600)
11-04 16:30:52.597 8530-8530/? W/MessageQueue:     at android.os.Handler.sendMessageDelayed(Handler.java:570)
11-04 16:30:52.597 8530-8530/? W/MessageQueue:     at android.os.Handler.sendEmptyMessageDelayed(Handler.java:534)
11-04 16:30:52.597 8530-8530/? W/MessageQueue:     at android.os.Handler.sendEmptyMessage(Handler.java:519)
11-04 16:30:52.597 8530-8530/? W/MessageQueue:     at ken.a(SourceFile:792)
11-04 16:30:52.597 8530-8530/? W/MessageQueue:     at kei.g(SourceFile:554)
11-04 16:30:52.597 8530-8530/? W/MessageQueue:     at keq.g(SourceFile:146)
11-04 16:30:52.597 8530-8530/? W/MessageQueue:     at lcu.c(SourceFile:488)
11-04 16:30:52.597 8530-8530/? W/MessageQueue:     at lwr.b(SourceFile:245)
11-04 16:30:52.597 8530-8530/? W/MessageQueue:     at lwy.b(SourceFile:148)
11-04 16:30:52.597 8530-8530/? W/MessageQueue:     at ldd.a(SourceFile:694)
11-04 16:30:52.597 8530-8530/? W/MessageQueue:     at alm.a(SourceFile:267)
11-04 16:30:52.597 8530-8530/? W/MessageQueue:     at alm.b(SourceFile:287)
11-04 16:30:52.597 8530-8530/? W/MessageQueue:     at auc.run(SourceFile:209)
11-04 16:30:52.597 8530-8530/? W/MessageQueue:     at android.os.Handler.handleCallback(Handler.java:739)
11-04 16:30:52.597 8530-8530/? W/MessageQueue:     at android.os.Handler.dispatchMessage(Handler.java:95)
11-04 16:30:52.597 8530-8530/? W/MessageQueue:     at android.os.Looper.loop(Looper.java:148)
11-04 16:30:52.597 8530-8530/? W/MessageQueue:     at android.app.ActivityThread.main(ActivityThread.java:5417)
11-04 16:30:52.597 8530-8530/? W/MessageQueue:     at java.lang.reflect.Method.invoke(Native Method)
11-04 16:30:52.597 8530-8530/? W/MessageQueue:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-04 16:30:52.597 8530-8530/? W/MessageQueue:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

YouTubePlayer 在后台发布时似乎存在一些问题.也许我做错了什么?

It seems there is some problem with YouTubePlayer release on back press. Maybe I'm doing something wrong?

我找到了解决方法:我将 onInitializationSuccess 中的 YouTubePlayer 实例存储在我的 Activity 中,并在新的 Fragment 初始化之前对其调用 release() ,之后一切正常,log cat 中没有错误.

I've found workaround: I'm storing YouTubePlayer instance from onInitializationSuccess in my Activity and call release() on it before new Fragment initialization and after that all goes right and there is no errors in log cat.

推荐答案

我已经扩展了 YouTubePlayerSupportFragment 来存储播放器实例并在 onDestroyView 中正确释放它

I've extend YouTubePlayerSupportFragment to store player instance and correctly release it in onDestroyView

public class YouTubePlayerFragment2 extends YouTubePlayerSupportFragment {
        private YouTubePlayer mPlayer;



        public static YouTubePlayerFragment2 newInstance(){
            return new YouTubePlayerFragment2();
        }

        @Override
        public void initialize(String s, YouTubePlayer.OnInitializedListener onInitializedListener) {
            YouTubePlayer.OnInitializedListener listener = new YouTubePlayer.OnInitializedListener() {
                @Override
                public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
                    onInitializedListener.onInitializationSuccess(provider, youTubePlayer, b);
                    mPlayer = youTubePlayer;
                    mPlayer.setShowFullscreenButton(false);
                }

                @Override
                public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
                    onInitializedListener.onInitializationFailure(provider, youTubeInitializationResult);
                }
            };
            super.initialize(s, listener);
        }

        @Override
        public void onDestroyView() {
            if(mPlayer!=null) {

                mPlayer.release();
            }
            super.onDestroyView();
        }
    }

这篇关于YouTubePlayerFragment 后台堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:YouTubePlayerFragment 后台堆栈

基础教程推荐