使用带有timeMillis参数的方法:
player.loadVideo (String videoId, int timeMillis)
在你的情况下:
player.loadVideo("xyoajjlPt_o", 36000);
//this will start the video from 36th second
[编辑]
嗯,你可以利用 Handler 来跟踪视频的当前运行时间。当以毫秒为单位的时间达到您想要停止视频的点时,只需调用player.pause() 方法即可。
这是活动的完整代码:
public class MyActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener{
public static final String API_KEY = "YOUR_API_KEY_HERE";
public static final String VIDEO_ID = "yeF_b8EQcK0";
private static YouTubePlayer player;
TextView text;
//this is the end time in milliseconds (65th second)
public int endTime = 65000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
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, 36000);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//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);
}
}
附:有人问了一个重复的问题here,我已经在那里更新了我的answer。