在YouTubePlayerView中最小化/最大化youTube视频

时间:2018-04-05 05:13:06

标签: android android-youtube-api

我有一个容器,我可以在其中显示GIF视频。点击GIF,它会在YouTube中打开相同的视频。 YouTubePlayerView具有最大化和最小化图标,当我点击该视频时只是重新启动而不是最小化它并返回到同一个容器。我不确定如何最小化在YouTubePlayerView中播放的视频。

我创建了一个名为YouTubeACtivity的独立活动:

public class YoutubeActivity extends AppCompatActivity {

    String shortCode = "";

    public static void show(Activity activity, String shortCode)
    {
        Intent intent = new Intent(activity, YoutubeActivity.class);
        intent.putExtra(Constants.YOUTUBE_VIDEO, shortCode);
        activity.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_youtubeplayer);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        shortCode = getIntent().getStringExtra(Constants.YOUTUBE_VIDEO);


            YouTubePlayerFragment mYoutubePlayerFragment = new YouTubePlayerFragment().newInstance();

            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.youtubeContainer, mYoutubePlayerFragment);
            ft.commit();

            if (mYoutubePlayerFragment != null) {
                mYoutubePlayerFragment.initialize(getResources().getString(R.string.google_api_key), new YouTubePlayer.OnInitializedListener() {
                    @Override
                    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean restored) {
                        //youTubePlayer.setFullscreen(true);
                        youTubePlayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE| YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION);
                       // youTubePlayer.  setShowFullscreenButton(false);

                        youTubePlayer.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {

                            @Override
                            public void onFullscreen(boolean fullscreen) {

                              //  onBackPressed();

                            }
                        });


                        Log.e("shortcode ", "reached" + shortCode);
                        if (shortCode != null) {

                            if (restored) {
                                youTubePlayer.play();
                            } else {
                                youTubePlayer.loadVideo("" + shortCode + "");
                            }
                        }

                    }

                    @Override
                    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

                        Log.e("youtube initialization ", "initialisation fails");
                    }
                });
            }

    }


}

GIF视频显示在实现SurfaceHolder回调的FrameLayout中。

我只是想知道,无论如何我们可以最大限度地减少视频并回归容器布局。

1 个答案:

答案 0 :(得分:0)

首先我建议使用播放器对象进入或退出全屏

youtubeplayer.setFullScreen(true/false);

然后在包含容器的活动的清单中添加配置更改

android:configChanges="orientation|screenSize|keyboardHidden|screenLayout"

据我所知,你的活动正在重新启动你的问题