使用新的youtube API库获取youtube视频时间

时间:2013-10-11 19:53:18

标签: android api time youtube elapsed

我想使用新的Android Youtube API播放YouTube视频。我从谷歌(YouTubeAndroidAPIDemo)下载了演示项目,这是我用来发布视频的代码:

Intent intent = YouTubeStandalonePlayer.createVideoIntent(this, getString(R.string.ID_DEVELOPER_KEY), "cdgQpa1pUUE", 0, true, false);
startActivityForResult(intent, 1);

这运行youtube视频确定,但是,当用户按下后退键时,我想知道时间已过(例如,YouTube视频大小是3:01,但用户只看了50秒) 我怎么知道时间过去了? 谢谢!

1 个答案:

答案 0 :(得分:0)

最后,我让我的目标创建另一个活动,将我想要启动的YouTube视频的ID放入一个包中,并添加一个YouTubePlayer类的atributte,将其加载到onInitializationSuccess函数中。这是我的代码:

公共课Youtube_Activity扩展YouTubeBaseActivity实施YouTubePlayer.OnInitializedListener {

private String mVideo;
private int mInicio;
private YouTubePlayerView mYouTubeView;
private YouTubePlayer mPlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.youtube_activity);
    Bundle extra = getIntent().getExtras();
    mVideo = extra.getString("video");
    mYouTubeView = (YouTubePlayerView)findViewById(R.id.youtube_view);
    mYouTubeView.initialize(getString(R.string.ID_DEVELOPER_KEY), this);
}

@Override
public void onInitializationFailure(Provider provider,YouTubeInitializationResult error) {
    Toast.makeText(this, "Oh no! "+error.toString(),Toast.LENGTH_LONG).show();
}

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

@Override
public void finish() {
    Intent data = new Intent();
        int t = (mPlayer.getCurrentTimeMillis()/1000);
        data.putExtra("tiempo",t );
    setResult(0, data);
    super.finish();
}