我正在使用新的Youtube API for android。我已设法使用API显示特定的播放列表(使用ListView),点击列表中的视频后,我就可以播放该特定视频。
我的问题是这个。如何获取特定视频的详细信息?视频标题和视图数量等详细信息。我试过看YoutubeThumbnailView / loader,YoutubePlayerView,YoutubePlayer但没有运气。
这里是主要活动的代码,如果您需要更多信息,请告诉我。
非常感谢任何帮助。
public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener, YouTubeThumbnailView.OnInitializedListener{
private final static String PLAY_LIST_ID = "XXX";
private final static String DEV_KEY = "XXX";
private YouTubeThumbnailView thumbnailView;
private YouTubeThumbnailLoader loader;
private ArrayList<HashMap<String, YouTubeVideo>> arrayList;
private ListView listView;
private SpecialAdapter adapter;
private int counter;
private YouTubePlayerView playerView;
private YouTubePlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] keys = new String[]{"key"};
int[] values = new int[]{R.id.imageView1};
arrayList = new ArrayList<HashMap<String, YouTubeVideo>>();
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.listView1);
listView.setOnItemClickListener(new onListClickListener());
playerView = (YouTubePlayerView)findViewById(R.id.player);
thumbnailView = new YouTubeThumbnailView(this);
thumbnailView.initialize(DEV_KEY, this);
playerView.initialize(DEV_KEY, this);
adapter = new SpecialAdapter(this, arrayList, R.layout.row_layout, keys, values);
}
class onListClickListener implements OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
playerView.setVisibility(View.VISIBLE);
player.cueVideo(arrayList.get(arg2).get("key").videoID);
}
}
@Override
public void onInitializationSuccess(YouTubeThumbnailView arg0,
YouTubeThumbnailLoader arg1) {
loader = arg1;
loader.setPlaylist(PLAY_LIST_ID);
loader.setOnThumbnailLoadedListener(new OnThumbnailLoadedListener() {
@Override
public void onThumbnailLoaded(YouTubeThumbnailView arg0, String videoId) {
HashMap<String, YouTubeVideo> map;
ImageView imageView;
if(loader.hasNext()){
map = new HashMap<String, YouTubeVideo>();
imageView = new ImageView(MainActivity.this);
imageView.setImageDrawable(arg0.getDrawable());
map.put("key", new YouTubeVideo(imageView, videoId));
arrayList.add(map);
loader.next();
}else {
map = new HashMap<String, YouTubeVideo>();
imageView = new ImageView(MainActivity.this);
imageView.setImageDrawable(arg0.getDrawable());
map.put("key", new YouTubeVideo(imageView, videoId));
arrayList.add(map);
listView.setAdapter(adapter);
}
}
@Override
public void onThumbnailError(YouTubeThumbnailView arg0, ErrorReason arg1) {
}
});
}
@Override
public void onInitializationFailure(YouTubeThumbnailView arg0,
YouTubeInitializationResult arg1) {
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onInitializationFailure(Provider arg0,
YouTubeInitializationResult arg1) {
}
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean wasRestored) {
this.player = player;
}
@Override
protected void onDestroy() {
if(loader != null) {
loader.release();
}
super.onDestroy();
}
}