Android Youtube api播放器不断停止

时间:2016-08-01 04:31:32

标签: android video youtube youtube-api android-youtube-api

我正在使用youtube api播放器在我的应用程序中播放视频。

    youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    transaction.add(R.id.youtube_view, youTubePlayerFragment).commit();

    playerStateChangeListener = new MyPlayerStateChangeListener();
    playbackEventListener = new MyPlaybackEventListener();

    youTubePlayerFragment.initialize(ApplicationConstants.YOUTUBE_API_KEY, new YouTubePlayer.OnInitializedListener() {

        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
            mplayer = player;
            player.setFullscreen(false);
            player.setPlayerStateChangeListener(playerStateChangeListener);
            player.setPlaybackEventListener(playbackEventListener);
            player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE);
            if (!wasRestored) {
                player.cueVideo(url); // Plays https://www.youtube.com/watch?v=fhWaJi1Hsfo
            }
        }

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

我正在更改设备配置更改中的播放器角色,

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    int config = newConfig.orientation;
    FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mYoutubeView.getLayoutParams();
    switch (config){
        case  Configuration.ORIENTATION_LANDSCAPE :
            mplayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE);
            params.height = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 300,getResources().getDisplayMetrics());
            params.width = LinearLayout.LayoutParams.WRAP_CONTENT ;//(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 375,getResources().getDisplayMetrics());
            mYoutubeView.setLayoutParams(params);
            break;
        case  Configuration.ORIENTATION_PORTRAIT :
            params.height = LinearLayout.LayoutParams.WRAP_CONTENT;
            params.width = LinearLayout.LayoutParams.MATCH_PARENT;
            mYoutubeView.setLayoutParams(params);
            break;
    }
}

使用此标志我可以停止播放器api player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE);

中的连续停止

但问题是,

  1. 在播放视频时如果用户最小化应用程序(按设备主页按钮)并在设置中更改设备字体大小 - > font-size
  2. 重新打开最小化,转到视频并将方向更改为水平,它会显示我之前播放的视频,然后最小化应用程序。
  3. 如果我删除了player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE);标记,则可以避免此行为,但视频会不断停止。

1 个答案:

答案 0 :(得分:1)

尝试使用loadVideo()而不是cueVideo()