youtube播放器在更改为全屏模式时停止

时间:2018-06-07 18:15:51

标签: android youtube-api

目前,我正在使用youtube api在我的应用上显示youtube视频并且效果很好,但每当我更改为全屏模式时,视频都会停止,我需要按播放按钮再次启动它。

这可能听起来不是一个不重要的问题,但对我的应用程序来说这非常重要,所以我真的很感激任何解决方案。

这是我的代码:

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_youtube_player);

        final int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;


        getWindow().getDecorView().setSystemUiVisibility(flags);


        final View decorView = getWindow().getDecorView();
        decorView
                .setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {

                    @Override
                    public void onSystemUiVisibilityChange(int visibility) {
                        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                            decorView.setSystemUiVisibility(flags);
                        }
                    }
                });

        videoID = getIntent().getStringExtra("video_id");
        youTubePlayerView = findViewById(R.id.youtube_player_view);
        initializeYoutubePlayer();
        String [] temp = getIntent().getStringArrayExtra("playlist_order");
        if(temp != null)
            order = Arrays.copyOf(temp, temp.length);
        else {
            ImageView nextTrackButton = findViewById(R.id.nextTrackButton);
            ImageView lastTrackButton = findViewById(R.id.lastTrackButton);

            nextTrackButton.setVisibility(View.GONE);
            lastTrackButton.setVisibility(View.GONE);
        }


    }

    /**
     * initialize the youtube player
     */
    private void initializeYoutubePlayer() {
        youTubePlayerView.initialize(Constants.getKey(), new YouTubePlayer.OnInitializedListener() {

            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer,
                                                boolean wasRestored) {



                if (!wasRestored) {

                    youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);


                    youTubePlayer.loadVideo(videoID);


                    youTubePlayer.play();

                    youTubePlayer.setShowFullscreenButton(true);

                    myYouTubePlayer = youTubePlayer;

                    myYouTubePlayer.setPlayerStateChangeListener(new YouTubePlayer.PlayerStateChangeListener() {
                        @Override
                        public void onLoading() {

                        }

                        @Override
                        public void onLoaded(String s) {

                        }

                        @Override
                        public void onAdStarted() {

                        }

                        @Override
                        public void onVideoStarted() {

                        }

                        @Override
                        public void onVideoEnded() {
                            int nextI = 0;
                            for (int i = 0; i < order.length; i++) {
                                if (order[i].equals(videoID)) {
                                    nextI = i + 1;
                                    break;
                                }

                            }

                            if (nextI >= order.length) {
                                nextI = 0;
                            }

                            videoID = order[nextI];
                            myYouTubePlayer.loadVideo(videoID);
                        }

                        @Override
                        public void onError(YouTubePlayer.ErrorReason errorReason) {

                        }
                    });

                }
            }

            @Override
            public void onInitializationFailure(YouTubePlayer.Provider arg0, YouTubeInitializationResult arg1) {
                //print or show error if initialization failed
                Log.e(TAG, "Youtube Player View initialization failed");
            }
        });
    }

    @SuppressLint("NewApi")
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
    } 

2 个答案:

答案 0 :(得分:0)

Youtube API Android有一个here。我认为你可以使用它并使其在全屏显示时继续play()

答案 1 :(得分:0)

将此行添加到您的清单文件

<activity android:name=".MainActivity"
        android:configChanges="orientation|screenSize"
        >
    </activity>