使用API​​ 3.0在Android中获取带有视频ID的YouTube标题

时间:2016-02-22 22:40:38

标签: php android youtube youtube-api android-youtube-api

我一直在尝试某种方式,并寻找一整天。我能找到的最短路径是将videoID放在YouTube播放器中,然后获取其标题。但我确信有比这更短/更干净的方式。

我也接受使用PHP而不是API,但我真的不知道因为方法发生了变化。

提前致谢。

2 个答案:

答案 0 :(得分:0)

我是用PHP做的。我仍然想知道如何使用API​​ 3.0做到这一点,但这看起来更容易。该方法只获取一个字符串,分析它(获取ID),然后将其发送到与PHP文件建立连接的asynktask,并将字符串检索到EditText。

您并不真正需要PHP文件,因为您可以从YouTube本身获取此文件。您始终需要的是YouTube API密钥。

这是Java方法:

private void getVideoTitle(String t) {
    String id = "---";
    int i = 0;
    if (t.length() == 11) {
        id = t;
    }
    else {
        i = t.toLowerCase().indexOf("https://youtu.be/".toLowerCase());
        if (i > -1) {
            id = t.substring(i + 17, i + 28);
        }
        else {
            i = t.toLowerCase().indexOf("v=".toLowerCase());
            if (i > -1) {
                id = t.substring(i + 2, i + 13);
            }
        }
    }

    class YTF extends AsyncTask<String, Void, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            edtTitle.setText(s);
        }

        @Override
        protected String doInBackground(String... p) {
            BufferedReader br = null;
            try {
                URL url = new URL("personal php file that gets title from id?id=" + p[0]);
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                br = new BufferedReader(new InputStreamReader(con.getInputStream()));
                return br.readLine();
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
                return null;
            }
        }
    }
    if(id.length()>3){
        YTF ytf = new YTF();
        ytf.execute(id);
    }
    else{
        Snackbar.make(getWindow().getDecorView(), "Not a YT Link", Snackbar.LENGTH_SHORT).setAction("YT", null).show();
    }
}

这是PHP文件:

<?php
    $data = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=".$_GET['id']."&key=1234567890&fields=items(id,snippet(title),statistics)&part=snippet,statistics");
    $json = json_decode($data, true);
    $videoTitle = $json['items'][0]['snippet']['title'];
    if(empty($videoTitle)){echo "Uops.";}else{echo $videoTitle;}
?>

我希望这有助于某人。

答案 1 :(得分:0)

你试过这个吗? https://developers.google.com/youtube/v3/getting-started此处显示了各种示例。

URL: https://www.googleapis.com/youtube/v3/videos?id=7lCDEYXw3mM&key=YOUR_API_KEY
     &part=snippet,contentDetails,statistics,status

Description: This example retrieves a video resource and identifies several
             resource parts that should be included in the API response.

API response:

{
 "kind": "youtube#videoListResponse",
 "etag": "\"UCBpFjp2h75_b92t44sqraUcyu0/sDAlsG9NGKfr6v5AlPZKSEZdtqA\"",
 "videos": [
  {
   "id": "7lCDEYXw3mM",
   "kind": "youtube#video",
   "etag": "\"UCBpFjp2h75_b92t44sqraUcyu0/iYynQR8AtacsFUwWmrVaw4Smb_Q\"",
   "snippet": {
    "publishedAt": "2012-06-20T22:45:24.000Z",
    "channelId": "UC_x5XG1OV2P6uZZ5FSM9Ttw",
    "title": "Google I/O 101: Q&A On Using Google APIs",
    "description": "Antonio Fuentes speaks to us and takes questions on working with Google APIs and OAuth 2.0.",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/7lCDEYXw3mM/default.jpg"
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/7lCDEYXw3mM/mqdefault.jpg"
     },
     "high": {
      "url": "https://i.ytimg.com/vi/7lCDEYXw3mM/hqdefault.jpg"
     }
    },
    "categoryId": "28"
   },
   "contentDetails": {
    "duration": "PT15M51S",
    "aspectRatio": "RATIO_16_9"
   },
   "statistics": {
    "viewCount": "3057",
    "likeCount": "25",
    "dislikeCount": "0",
    "favoriteCount": "17",
    "commentCount": "12"
   },
   "status": {
    "uploadStatus": "STATUS_PROCESSED",
    "privacyStatus": "PRIVACY_PUBLIC"
   }
  }
 ]
}