我工作的机器人Youtube Api.i成功创建了project.i可以播放视频,也可以点击按钮跳过视频。现在我添加了第二个按钮,在这个按钮的点击我想在onInitializationSuccess方法中更改视频ID 这是我的代码
public class MainActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {
static private final String DEVELOPER_KEY = "***********";
private String VIDEO = "*****", VIDEO1 = "*****";
private Button b1, b2;;
private YouTubePlayer player1;
private PlayerStyle style;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
youTubeView.initialize(DEVELOPER_KEY, this);
youTubeView.setEnabled(false);
style = PlayerStyle.CHROMELESS;
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int[] array = new int[] { 36000, 140000, 250000 };
Random random = new Random();
final int index = array[random.nextInt(array.length)];
Log.e("randommmmmmmmmmmm", String.valueOf(index));
player1.seekToMillis(index);
}
});
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int[] array = new int[] { 36000, 140000, 250000 };
Random random = new Random();
final int index = array[random.nextInt(array.length)];
Log.e("randommmmmmmmmmmm", String.valueOf(index));
// player1.loadVideo(VIDEO, index);
player1.seekToMillis(index);
}
});
}
@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) {
this.player1 = player;
player1.setPlayerStyle(style);
player.loadVideo(VIDEO);
if(b2.isFocused())
player.loadVideo(VIDEO1);
}
}
我尝试用
来解决我的问题player.loadVideo(VIDEO,index);
但在这种情况下,videoID已更改,但每次单击按钮时都会加载视频 如何在按钮点击中更改视频ID,每次都不加载视频?