YouTube コンテンツ (最も重要なのはサムネイル) を参照して表示できる Android アプリケーションを実装しようとしています。
通常は Youtube API と Gdata を使用しますが、残念ながら Android ではうまく機能しません。
したがって、Java 用の Google API クライアント ライブラリ (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);
}
}
コードはかなり粗雑ですが、ここまでは機能しますが、URL から画像を取得しようとすると失敗します。
ビデオのデータ(サムネイルなど)に最もよくアクセスする方法、または一般的にこの問題に対処する方法について、洞察や提案はありますか?残念ながら、Android プラットフォームでの YouTube へのアクセスは、めったに話題に上ることはありません。
よろしくお願いします!