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



我一直在尝试一些方法,并搜索了一整天。我能找到的最短方法是将videoID放在YouTube播放器中,然后获取其标题。但我相信有比这更短/更干净的方法。

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

提前谢谢。

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

你真的不需要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;}
?>

我希望这对某人有所帮助。

你试过这个吗? 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"
   }
  }
 ]
}

相关内容

  • 没有找到相关文章

最新更新