Youtube播放器在View Pager内的每一秒都暂停了

时间:2016-06-16 08:13:26

标签: android android-viewpager android-youtube-api

我已在 ViewPager 中使用 YouTubePlayerSupportFragment 实施了一个Youtube播放器,我已成功完成此操作。我得到的问题是 YoutubePlayer 每当我试图播放它时,每秒都会暂停。但是,如果我在全屏播放,那么它的播放效果很好,但在查看寻呼机内部有问题。 YouTubePlayerSupportFragment 的代码是:

private void initYoutubeFragment() {
        final YouTubePlayerSupportFragment youTubePlayerFragment =
                YouTubePlayerSupportFragment.newInstance();
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.add(R.id.fl_coach_profile_youtube, youTubePlayerFragment).commit();
        youTubePlayerFragment.initialize(getString(R.string.youtube_player_api_key)
                , new YouTubePlayer.OnInitializedListener() {
                    @Override
                    public void onInitializationSuccess(YouTubePlayer.Provider provider,
                                                        YouTubePlayer player, boolean wasRestored) {
                        mbIsVideoRestored = wasRestored;
                        if (!wasRestored) {
                            mYoutubePlayer = player;
                            mYoutubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
                            mYoutubePlayer.setPlaybackEventListener(mPlaybackEventListener);
                            mYoutubePlayer.setPlayerStateChangeListener(mPlayerStateChangeListener);
                        } else {
                            player.play();
                        }
                    }

                    @Override
                    public void onInitializationFailure(YouTubePlayer.Provider provider,
                                                        YouTubeInitializationResult error) {
                    }
                });
    }

播放器的听众

// Youtube player event listener
    private YouTubePlayer.PlaybackEventListener mPlaybackEventListener = new YouTubePlayer.PlaybackEventListener() {

        @Override
        public void onBuffering(boolean arg0) {
        }

        @Override
        public void onPaused() {

        }

        @Override
        public void onPlaying() {

        }

        @Override
        public void onSeekTo(int arg0) {
        }

        @Override
        public void onStopped() {

        }

    };

    // Youtube player state change listener
    private YouTubePlayer.PlayerStateChangeListener mPlayerStateChangeListener = new YouTubePlayer.PlayerStateChangeListener() {

        @Override
        public void onAdStarted() {
        }

        @Override
        public void onError(YouTubePlayer.ErrorReason arg0) {
        }

        @Override
        public void onLoaded(String arg0) {
        }

        @Override
        public void onLoading() {

        }

        @Override
        public void onVideoEnded() {
        }

        @Override
        public void onVideoStarted() {

        }
    };

1 个答案:

答案 0 :(得分:1)

在转移到另一个Fragment之前使用release()方法:

youTubePlayer.release();