我試圖實現可以瀏覽和顯示YouTube內容(最重要的是縮略圖)的Android應用程序。使用「用於Java的Google API客戶端庫」訪問YouTube視頻數據
現在通常情況下,通過Youtube API和Gdata的方式,但不幸的是,它們不能很好地與Android搭配。
因此,我試圖使用谷歌API客戶端庫的Java(http://code.google.com/p/google-api-java-client/)
這顯然與Android完全兼容。不幸的是,它的發展還很早,所以沒有太多的信息。
我已經成功地查詢了YouTube的數據庫並返回了標題和描述等數據,但是我在返回縮略圖方面遇到了很多麻煩。說實話,我不太清楚查詢過程是如何工作的。
這裏是代碼的主要部分:
HttpTransport transport = new NetHttpTransport();
final JsonFactory jsonFactory = new JacksonFactory();
final JsonCParser parser = new JsonCParser(jsonFactory);
HttpRequestFactory factory = transport.createRequestFactory(new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) throws IOException {
// TODO Auto-generated method stub
GoogleHeaders headers = new GoogleHeaders();
headers.setApplicationName("1Google-YouTubeSample/1.0");
headers.gdataVersion = "2";
request.setHeaders(headers);
request.addParser(parser);
}
});
YouTubeUrl url = new YouTubeUrl("https://gdata.youtube.com/feeds/api/videos");
url.author = "whatever";
url.maxResults = 2;
HttpRequest request;
try {
request = factory.buildGetRequest(url);
VideoFeed feed = request.execute().parseAs(VideoFeed.class);
for (Video video : feed.items) {
Log.v("ddarz", "video URL" + video.player.defaultUrl + "/default.jpg");
TextView tv1 = (TextView) findViewById(R.id.textView1);
tv1.setText(video.title);
TextView tv2 = (TextView) findViewById(R.id.textView2);
tv2.setText(video.description);
ImageView iv = (ImageView) findViewById(R.id.imageView1);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL("http://img.youtube.com/vi/bQVoAWSP7k4/1.jpg").getContent());
//iv.setImageBitmap(bitmap);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
及其類別:
public static class VideoFeed {
@Key List<Video> items;
int totalItems;
}
public static class Video {
@Key String title;
@Key String description;
@Key Player player;
@Key DateTime updated;
}
public static class Player {
@Key("default") String defaultUrl;
}
public static class YouTubeUrl extends GenericUrl {
@Key final String alt = "jsonc";
@Key String author;
@Key("max-results") Integer maxResults;
YouTubeUrl(String url) {
super(url);
}
}
的代碼是相當粗糙,但是這一點它的工作原理,它試圖獲取失敗時來自網址的圖片。
有沒有人有任何關於如何最好地訪問視頻數據(縮略圖等)的洞察力和/或建議或一般如何解決這個問題?不幸的是,Android平臺上的YouTube訪問很少涉及到問題。
非常感謝提前!
我也一直在檢索視頻預覽縮略圖。也許我們可以嘗試解析縮略圖的請求?不過,我只能看到一個gzip壓縮請求,其中包含沒有適當的信息:( –