使用Youtube Player API播放不同的youtube视频 [英] Playing different youtube videos using Youtube Player API
问题描述
我正在使用适用于Android的Youtube Player API播放视频,并选择listview
中的视频之一(视频之前已解析json等).当我只能播放选择的listview
的第一个视频时,就会出现问题,因为当我单击listView的其他元素时,播放器视图不会改变.这是重要的代码:
I´m using the Youtube Player API for Android to play videos selecting one of these in a listview
(videos are previously got parsing json etc etc). The problem comes when I can only play the first video of the listview
that I select, cause the player view doesnt change when I do click in a different element of the listView. This is the important code:
lista.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> pariente, View view,
int posicion, long id) {
Video chosen = (Video)pariente.getItemAtPosition(posicion);
String urlVideo = chosen.getUrl();
String aux = getYoutubeVideoId(urlVideo);
URL_VIDEO = aux;
youTubeView.initialize(KEY_DEVELOPER, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationFailure(Provider arg0,
YouTubeInitializationResult arg1) {
// TODO Auto-generated method stub
Log.d("DEPURANDO: ERROR AL VISUALIZAR", URL_VIDEO);
}
@Override
public void onInitializationSuccess(Provider arg0,
YouTubePlayer player, boolean wasRestored) {
// TODO Auto-generated method stub
if(!wasRestored){
Log.d("YOUTUBE", "URL: " + URL_VIDEO);
player.cueVideo(URL_VIDEO);
}
}
});
}
我知道问题是因为播放器无法初始化两次,所以执行只在OnInitializationSucess
中输入了一次(我测试过),因此只能播放第一个视频.如何解决这个问题?
I understand the problem comes because the player can't be initialized twice, so the execution only enter once in OnInitializationSucess
(I tested) and therefore only plays the first video. how to solve this problem?
推荐答案
检查YouTube API示例的最新版本
Check the last version of the YouTube API Samples
我认为该示例可能对您有用:
I think this sample can be useful to you: