0

I want to play youtube videos using new Android Youtube API. I have download demo project from google (YouTubeAndroidAPIDemo) and this is the code I use to launch a video:

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

This running the youtube video ok, but, when the user press back key, I want to know time elapsed (for example, the youtube video size is 3:01, but the user only have seen 50 seconds) How can I know time elapsed? Thanks!

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148

1 Answers1

0

Finally I got my target creating another activity, put in a bundle the ID of the youtube video that I want to launch, and adding an atributte of the YouTubePlayer class, loading it in the onInitializationSuccess function. Here is my code:

public class Youtube_Activity extends YouTubeBaseActivity implements 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();
}