2014-09-25 110 views
1

我使用YouTube API播放我的視頻,在YouTube播放器中有play()pause()函數。我需要在特定時間停止視頻。我正在使用pause(),但視頻的背景緩衝。如何在特定的時間限制後停止緩衝視頻?在WebView中存在URL一個選項開始和結束標記緩衝像下面,如何停止youtube視頻或在android中設置緩衝區限制android

https://www.youtube.com/v/K_AdxJWFUh4&start=0&end=30

注:我使用YouTube播放器播放視頻沒有的WebView

回答

2

檢查這代碼

public class YouTube extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener { 

    public static final String API_KEY = "<---YOUR KEY--->"; 
    public static final String VIDEO_ID = "<--VIDEO ID YOU WANT TO PLAY--->"; 
    private static YouTubePlayer player; 

    TextView text; 

    //this is the end time in milliseconds (65th second) 
    public int endTime = 5600; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_my); 
     text = (TextView) findViewById(R.id.text); 
     YouTubePlayerView youTubePlayerView = (YouTubePlayerView)findViewById(R.id.youtubeplayerview); 
     youTubePlayerView.initialize(API_KEY, this); 
    } 

    @Override 
    public void onInitializationFailure(Provider provider, YouTubeInitializationResult result) { 
     Toast.makeText(getApplicationContext(), 
     "onInitializationFailure()", 
     Toast.LENGTH_LONG).show(); 
    } 

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

     MyActivity.player = player; //necessary to access inside Runnable 

     //start the video at 36th second 
     player.loadVideo(VIDEO_ID, 0); 

     final Handler handler = new Handler(); 
     handler.postDelayed(() -> { 
      //For every 1 second, check the current time and endTime 
      if(MyActivity.player.getCurrentTimeMillis() <= endTime) { 
       text.setText("Video Playing at " + MyActivity.player.getCurrentTimeMillis()); 
       handler.postDelayed(this, 1000); 
      } else { 
       handler.removeCallbacks(this); //no longer required 
       text.setText(" Reached " + endTime); 
       MyActivity.player.pause(); //and Pause the video 
      } 
     }, 1000); 
    } 
} 

佈局XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:orientation="vertical" 
    tools:context=".MainActivity" > 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="http://android-er.blogspot.com/" 
     android:textStyle="bold" 
     android:layout_gravity="center_horizontal" 
     android:autoLink="web" /> 

    <com.google.android.youtube.player.YouTubePlayerView 
     android:id="@+id/youtubeplayerview" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

</LinearLayout>