2016-08-01 103 views
0

我使用YouTube的API播放器播放我的應用程序中的視頻。Android的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) { 
     } 

我使用這個標誌,我可以停止播放器API的連續停止改變對設備配置更改的球員助攻,

@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; 
    } 
} 

player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE);

但問題是,

  1. 在播放視頻時,如果用戶最小化應用程序(按設備主頁按鈕)a nd在設置中更改設備字體大小 - >字體大小
  2. 重新打開最小化,轉到視頻並將方向更改爲水平,它將顯示之前播放的視頻,然後纔將應用程序最小化。
  3. 任何如何行爲可以避免,如果我刪除player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE);標誌,但然後視頻將停止連續。

回答

1

嘗試使用loadVideo()代替cueVideo()