我在应用程序中使用YouTubeAndroidPlayerapi播放youtube视频。我想从中间开始视频,但无法找到(开始和结束)的方法,因为我们可以在youtube api中找到。
https://www.youtube.com/v/BmOpD46eZoA?start=36&end=65
在YouTubeAndroidPlayerapi中,我有一个如下的视频播放代码。我无法从视频中间开始。我没有在播放器下面找到任何方法来给出起始值和结束值。
我找了很多办法,但都没有找到。请提出建议。
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean wasRestored) {
if (!wasRestored) {
// player.cueVideo(VIDEO_ID);
player.loadVideo(xyoajjlPt_o);
}
使用带有timeMillis
参数的方法:
player.loadVideo (String videoId, int timeMillis)
在您的情况下:
player.loadVideo("xyoajjlPt_o", 36000);
//this will start the video from 36th second
[EDIT]
好吧,您可以使用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);
}
}
附言:这里问了一个重复的问题,我在那里更新了我的答案。