其他分享
首页 > 其他分享> > android-YouTubePlayerFragment堆栈

android-YouTubePlayerFragment堆栈

作者:互联网

我想在Backstack活动中使用YouTubePlayerFragment.因此,当我按后退按钮时,片段消失了,以后进行某些操作时(例如,单击按钮)又出现了另一个视频.这是我的代码

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中的混淆错误:

    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时存在一些问题.
也许我做错了什么?

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

解决方法:

我已经扩展了YouTubePlayerSupportFragment以存储播放器实例,并在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();
        }
    }

标签:android,android-youtube-api,youtube-api
来源: https://codeday.me/bug/20191011/1890812.html