屏幕方向不适用于youtubePlayerfragment

时间:2017-03-08 11:58:57

标签: android youtube-api

目前正在开发使用youtube api显示视频的Android应用程序。在这个应用程序中,我使用youtubeplayerfragment以片段形式显示视频。当我点击全屏视图方向时,屏幕在全屏模式下变为横向模式,当我点击从全屏模式退出时,屏幕仍保持横向模式,甚至尝试通过旋转设备旋转,每次屏幕方向改变视频开始从开始。如何保持方向变化的视频状态以及如何解决我的问题。

我的youtube代码在这里:

if (savedInstanceState == null) {
    mYouTubePlayerFragment.initialize(Constants.YOUTUBEDEVELOPERKEY,
        new YouTubePlayer.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider,
                YouTubePlayer youTubePlayer, boolean b) {


                //on player full screen mode
                youTubePlayer.setShowFullscreenButton(true);
                youTubePlayer.setOnFullscreenListener(
                    new YouTubePlayer.OnFullscreenListener() {
                        @Override
                        public void onFullscreen(boolean b) {
                            Log.d(TAG, String.valueOf(b));
                        }
                    });

                //play video in the player
                if (!b) {
                    youTubePlayer.loadVideo(args.getString(Constants.VIDEOID));
                    youTubePlayer.play();
                } else {
                    youTubePlayer.play();
                }
            }

            @Override
            public void onInitializationFailure(
                YouTubePlayer.Provider provider,
                YouTubeInitializationResult youTubeInitializationResult) {
                Log.d("FeedVideoFragment", youTubeInitializationResult.toString());
                youTubeInitializationResult.getErrorDialog(getActivity(), 0);
            }
        });
    getActivity().getSupportFragmentManager().beginTransaction().add(R.id.youtube_fragment,
        mYouTubePlayerFragment, MYYOUTUBEFRAGMENT).commit();

} else {

    mYouTubePlayerFragment.initialize(Constants.YOUTUBEDEVELOPERKEY,
        new YouTubePlayer.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider,
                YouTubePlayer youTubePlayer, boolean b) {


                //on player full screen mode
                youTubePlayer.setShowFullscreenButton(true);
                youTubePlayer.setOnFullscreenListener(
                    new YouTubePlayer.OnFullscreenListener() {
                        @Override
                        public void onFullscreen(boolean b) {

                        }
                    });

                //play video in the player
                if (!b) {
                    youTubePlayer.loadVideo(savedInstanceState.getString("videoId"));
                    youTubePlayer.play();
                } else {
                    youTubePlayer.play();
                }
            }

            @Override
            public void onInitializationFailure(
                YouTubePlayer.Provider provider,
                YouTubeInitializationResult youTubeInitializationResult) {
                Log.d("FeedVideoFragment", youTubeInitializationResult.toString());
                youTubeInitializationResult.getErrorDialog(getActivity(), 0);
            }
        });
    getActivity().getSupportFragmentManager().findFragmentByTag(MYYOUTUBEFRAGMENT);


}

1 个答案:

答案 0 :(得分:1)

用于保存视频状态    你的android:configChanges="keyboardHidden|orientation|screenSize"    您的片段出现的活动标记,例如:

<activity android:name=".activities.MainActivity"
   android:configChanges="keyboardHidden|orientation|screenSize">
在你的onFullscreen方法中

检查是否设置了播放器全屏

@Override
       public void onFullscreen(boolean b) {
           Log.e("onFullscreen",""+b);
           if(b)
               fullscreen=1;
           else fullscreen=2;
       } 

在你的背压方法中检查是否设置了全屏,然后设置为假

 @Override
           public void onBackPressed() {
               if(fullscreen==1)
               {
                   if(this.player!=null)
                       youTubePlayer.setFullscreen(false);
               }
               else
                   super.onBackPressed();
           } 

我希望它可以帮助你,抱歉我的英语不好