Youtube YouTubePlayerSupportFragment无法正常工作(始终加载?)

时间:2015-10-06 09:41:29

标签: android youtube youtube-api

我在YouTube上遇到问题(始终加载)。

显然工作正常“但只是第一次”,我想说,第一个实例(第一次运行)正常工作并正确加载horitzontal和垂直,但是当更改片段 - >我的MainActivity中的fragmentManager.beginTransaction()(我在所有片段上都有函数将此方法调用到beginTransaction),然后我转到 - > beginTransaction YouTube - >视频总是加载:

示例:

第1步

第一次 - > MainActivity - > beginTransaction YouTube - >点击 - - >加载 正确地

第2步

第二次 - > MainActivity - > beginTransaction YouTube - >点击 - - >始终加载

关闭应用程序(菜单)并重复步骤1和2.

这是我的代码:

public void youtube(){

        youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();

        youTubePlayerFragment.initialize(API_KEY, new YouTubePlayer.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
                if (!wasRestored) {
                    youTubePlayer.cueVideo(VIDEO_ID);
                    youTubePlayers=youTubePlayer;
                }
                // Toast.makeText(getActivity(), "1", Toast.LENGTH_SHORT).show();
                youTubePlayer.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {
                    @Override
                    public void onFullscreen(boolean b) {
                        if(b){
                            //Toast.makeText(getActivity(), "1", Toast.LENGTH_SHORT).show();
                            MainActivity.isYoutubeReadyHoritzontal=true;
                            knowIfPortraitAndBackManyally=0;
                        }else {
                            //Toast.makeText(getActivity(), "2", Toast.LENGTH_SHORT).show();
                            getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                            knowIfPortraitAndBackManyally=1;
                            MainActivity.countIFexit=0;
                            //MainActivity.isYoutubeReadyHoritzontal=false;
                        }
                    }
                });
            }

            @Override
            public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
                if (errorReason.isUserRecoverableError()) {
                    errorReason.getErrorDialog(getActivity(), RECOVERY_DIALOG_REQUEST).show();
                } else {
                    String errorMessage = String.format("YouTube Error (%1$s)",
                            errorReason.toString());
                    Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show();
                }
            }

        });


        android.support.v4.app.FragmentManager fragmentManager = getChildFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.youtube_fragment,youTubePlayerFragment).commit();
        MainActivity.isYoutubeReadyHoritzontal=true;

}

XML

<FrameLayout
                        android:orientation="vertical"
                        android:layout_width="match_parent"
                        android:layout_height="fill_parent"

                        android:id="@+id/youtube_fragment" >

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,这是由于未能妥善处理以前的播放器实例造成的。确保在不再需要时调用youTubePlayer.release()