最后,我让我的目标创建另一个活动,将我想要启动的 youtube 视频的 ID 放入捆绑包中,并添加 YouTubePlayer 类的属性,将其加载到 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();
}