YouTubePlayerFragment backstack

前端 未结 1 810
温柔的废话
温柔的废话 2021-01-22 11:51

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) appear

相关标签:
1条回答
  • 2021-01-22 12:37

    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();
            }
        }
    
    0 讨论(0)
提交回复
热议问题