2015-04-26 174 views
4

我目前面臨的兩個主要問題,YouTube播放器播放2秒,並停止在全屏

  1. 我使用的是YouTube播放器,當它得到全屏幕上,它起着爲1 2秒後停下來。

  2. 當我點擊中間的「播放」按鈕時,它會再次緩衝。即使灰色條被填充到它的中心。

縱向模式下不會出現這些問題。

這裏是我的課,像一個位在YouTube API演示百米運動員

public class Video extends YouTubeFailureRecoveryActivity implements YouTubePlayer.OnFullscreenListener, Utils.OnGetUrlListener, View.OnClickListener { 

    static int AUTO_PLAY_DELAY = 1000; 

    static final int PORTRAIT_ORIENTATION = Build.VERSION.SDK_INT < 9 
      ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT 
      : ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT; 

    private LinearLayout mRootLayout; 

    /** 
    * * Youtube *** 
    */ 
    YouTubePlayerView mPlayerView; 
    YouTubePlayer mPlayer; 
    boolean mIsFullscreen; 
    String urlID; 

    /** 
    * * My *** 
    */ 
    RelativeLayout mContainer; 
    ImageView mBtPlay; 
    boolean mIsNeedSetFlags; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Utils.getInstance().setActivity(this); 

     setContentView(R.layout.video_main); 
     mIsNeedSetFlags = true; 



     mRootLayout = (LinearLayout) findViewById(R.id.video_root_layout); 

     mContainer = (RelativeLayout) findViewById(R.id.container); 

     mBtPlay = (ImageView) findViewById(R.id.video_play); 
     mBtPlay.setVisibility(View.INVISIBLE); 

     mPlayerView = (YouTubePlayerView) findViewById(R.id.player); 

     Intent intent = getIntent(); 


     doLayout(); 
    } 


    @Override 
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) { 
     mPlayer = player; 

     player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT); 
     player.setOnFullscreenListener(this); 

     if (mIsNeedSetFlags) { 
      mIsNeedSetFlags = false; 
      int controlFlags = player.getFullscreenControlFlags(); 
      setRequestedOrientation(PORTRAIT_ORIENTATION); 
      controlFlags |= YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE; 
      player.setFullscreenControlFlags(controlFlags); 
     } 

     if (!wasRestored) { 
      player.cueVideo(urlID); 
     } 
    } 

    @Override 
    protected YouTubePlayer.Provider getYouTubePlayerProvider() { 
     return mPlayerView; 
    } 

    private void doLayout() { 
     LinearLayout.LayoutParams playerParams = (LinearLayout.LayoutParams) mPlayerView.getLayoutParams(); 
     if (mIsFullscreen) { 
      playerParams.width = LinearLayout.LayoutParams.MATCH_PARENT; 
      playerParams.height = LinearLayout.LayoutParams.MATCH_PARENT; 

      mParallaxScrollView.setVisibility(View.GONE); 
     } else { 
      mParallaxScrollView.setVisibility(View.VISIBLE); 

      if (getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) { 
       ViewGroup.LayoutParams otherViewsParams = mParallaxScrollView.getLayoutParams(); 
       playerParams.width = otherViewsParams.width = MATCH_PARENT; 
       playerParams.height = WRAP_CONTENT; 
       mRootLayout.setOrientation(LinearLayout.VERTICAL); 
      } 
     } 
     mPlayerView.setLayoutParams(playerParams); 
    } 

    @Override 
    public void onFullscreen(boolean isFullscreen) { 
     mIsFullscreen = isFullscreen; 
     showPlayerAndPlay(); 
     doLayout(); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     doLayout(); 
    } 

    @Override 
    public void onGetUrlFinished(String videoUrl) { 
     urlID = videoUrl; 
     mBtPlay.setVisibility(View.VISIBLE); 
     mBtPlay.setOnClickListener(this); 
     mPlayerView.initialize(Utils.YOU_TUBE_DEV_KEY, this); 
    } 

    @Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
      case R.id.video_play: 
       showPlayerAndPlay(); 
       break; 
     } 
    } 


    private void showPlayerAndPlay() { 
     mPlayerView.setVisibility(View.VISIBLE); 
     mBtPlay.setVisibility(View.INVISIBLE); 

     if (!mPlayer.isPlaying()) 
      new android.os.Handler().postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        mPlayer.play(); 
       } 
      }, AUTO_PLAY_DELAY); 
    } 
} 
+0

編輯:我似乎把一個浮動菜單放在玩家之上,玩家必須是頂視圖。 – nirh216

回答

8

YouTube不允許其他的意見,又在其播放器視圖頂部覆蓋。

如果您檢查日誌,您還將看到一條警告消息,指出此限制以及有關哪個視圖(其ID)和重疊區域的更多信息。

1

一個很好的選擇是使用Exoplayer將視頻與視圖疊加。這不是在Android SDK的一部分,但它是由谷歌建議,其中包括Android開發者文檔中:

http://google.github.io/ExoPlayer/https://developer.android.com/guide/topics/media/exoplayer.html

Exoplayer讓你流的任何類型的影片,不僅Youtubes視頻。

這也是很好的提及在Youtube應用程序中使用Exoplayer。

+0

感謝gor替代它的好處。但我明白了。 ;) – nirh216

+0

@ nirh216你怎麼做..? –

+0

最簡單的方法是使用像https://github.com/brianwernick/ExoMedia這樣的庫,它們使用Exoplayer提供包裝來簡化音頻和視頻實現。 否則,你應該看看那裏的github repo https://github.com/google/ExoPlayer上的Exoplayer演示,這是一個很好的起點。 –

0

正如答案所標記的正確解釋:問題是覆蓋Youtube播放器視圖的視圖。 如果你需要保持這些意見將Youtube正在初始化,那麼這將做伎倆

我正在爲涉及的視圖做交叉淡入淡出的加載動畫。將alpha設置爲0不會解決問題,因爲視圖仍然存在。但設置可見性GONEINVISIBLE確實有效。據我所知,如果不是VISIBLE,則不會計算出View,至少在可見性更改後不會考慮它。最後,我做了這樣的事情:

myView.animate().alpha(0).setDuration(800).setListener(
     new Animator.AnimatorListener() { 
       @Override 
       public void onAnimationStart(Animator animator) { 

       } 

       @Override 
       public void onAnimationEnd(Animator animator) { 
        myView.setVisibility(View.GONE); 
       } 

       @Override 
       public void onAnimationCancel(Animator animator) { 

       } 

       @Override 
       public void onAnimationRepeat(Animator animator) { 

       } 
      }).start();