読者です 読者をやめる 読者になる 読者になる

【Android】YouTube APIを使ってオリジナルアプリをつくる

Android

 

最初にAPIの基本系をご説明します。

 

返却値の指定(id,snippet)

https://www.googleapis.com/youtube/v3/search?key={API_KEY}&q=サッカー&part=id,snippet

 

50件まで
https://www.googleapis.com/youtube/v3/search?key={API_KEY}&q=サッカー&part=id&maxResults=50

 

次頁
https://www.googleapis.com/youtube/v3/search?key={API_KEY}&q=サッカー&part=id&pageToken=CAUQAA

 

20分12秒から
https://www.googleapis.com/youtube/v3/search?key={API_KEY}&q=サッカー&part=id&t=20m12s

---------------------------------------------------------------------------------------------------------

▼取得

String apikey = "Google Developerで払いだしてください";
String url = "https://www.googleapis.com/youtube/v3/search?key=%s&q=%s&part=id,snippet&maxResults=20";
url = String.format(url, apikey, searchWord);

 

Request request = new Request.Builder().url(url).build();
OkHttpClient client = new OkHttpClient();
Response response = client.newCall(request).execute();
String result = response.body().string();
JSONObject resJson = new JSONObject(result);
JSONArray items = resJson.getJSONArray("items");
int count = items.length()-1;
for (int i = 0; i < items.length(); i++) {
  JSONObject item = items.getJSONObject(i);
  //id
  String id = item.getString("id");
  JSONObject idJson = new JSONObject(id);
  String videoId = idJson.getString("videoId");

  //snippet
  String snippet = item.getString("snippet");
  JSONObject snippetJson = new JSONObject(snippet);
  String title = snippetJson.getString("title");

  String thumbnails = snippetJson.getString("thumbnails");
  JSONObject thumbnailsJson = new JSONObject(thumbnails);
  String medium = thumbnailsJson.getString("medium");
  JSONObject mediumJson = new JSONObject(medium);
  String thumbnailsUrl = mediumJson.getString("url");

  URL imageUrl = new URL(thumbnailsUrl);
  InputStream imageIs = imageUrl.openStream();
  Bitmap image = BitmapFactory.decodeStream(imageIs);

  ~サムネイルはBitmapでこの後保存するため変換しています

  ~

}

 

jsonレスポンス(正常)抜粋です

{
 "kind": "youtube#searchListResponse",
 "etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/Y9Um_sM0nFeuMmFy5NazU_fFisw\"",
 "nextPageToken": "CBQQAA",
 "regionCode": "JP",
 "pageInfo": {
 "totalResults": 1000000,
 "resultsPerPage": 20
 },
 "items": [
 {
 "kind": "youtube#searchResult",
 "etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/_8EUDwBtGJvkRifWLoxURBVS0Mo\"",
 "id": {
  "kind": "youtube#video",
  "videoId": "66G-yyeWFjE"
 },
 "snippet": {
  "publishedAt": "2016-06-30T15:21:09.000Z",
  "channelId": "UCa10nxShhzNrCE1o2ZOPztg",
  "title": "Jinco - Tokyo",
  "description": "Spotify: http://spoti.fi/29exMzZ",
  "thumbnails": {
   "default": {
    "url": "https://i.ytimg.com/vi/66G-yyeWFjE/default.jpg",
    "width": 120,
    "height": 90
   },
   "medium": {
    "url": "https://i.ytimg.com/vi/66G-yyeWFjE/mqdefault.jpg",
    "width": 320,
    "height": 180
   },
   "high": {
    "url": "https://i.ytimg.com/vi/66G-yyeWFjE/hqdefault.jpg",
    "width": 480,
    "height": 360
   }
  },
  "channelTitle": "Trap Nation",
  "liveBroadcastContent": "none"
 }

jsonレスポンス(異常)

{
 "error": {
 "errors": [
  {
  "domain": "global",
  "reason": "invalidParameter",
  "message": "Invalid value '100'. Values must be within the range: [0, 50]",
  "locationType": "parameter",
  "location": "maxResults"
  }
 ],
 "code": 400,
 "message": "Invalid value '100'. Values must be within the range: [0, 50]"
 }

}

---------------------------------------------------------------------------------------------------------

▼再生
String videoId = "kCNpOe5S1B4";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("vnd.youtube:" + videoId));
intent.putExtra("VIDEO_ID", videoId);
startActivity(intent);

 

 

以上です。