使用方法與timeMillis
參數:
player.loadVideo (String videoId, int timeMillis)
你的情況:
player.loadVideo("xyoajjlPt_o", 36000);
//this will start the video from 36th second
[編輯]
好了,你可以利用處理器來跟蹤視頻的當前運行時間。當以毫秒爲單位的時間達到您想停止視頻的時間時,只需撥打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。
可能的重複[如何在youtube Android播放器中爲youtube視頻提供開始和結束參數?](http://stackoverflow.com/questions/20565494/how-to-provide-start-and-end-parameters爲youtube-video-in-youtube-android-p) – JoelFernandes
你能爲上述問題建議嗎? – user1891910