【问题标题】:Android Get video link from youtubeAndroid 从 youtube 获取视频链接
【发布时间】:2023-03-28 04:51:01
【问题描述】:

嗨,我正在开发一个 android 应用程序,我的应用程序的一部分想要将歌曲标题解析到 youtube 并获取视频链接。获得 100% 正确的视频并不重要。那么我如何从 youtube 中检索数据?

任何人都可以帮助我找到一个解决方案,它对我来说真的很有帮助。

谢谢

【问题讨论】:

    标签: android youtube-api


    【解决方案1】:

    最常见的方法是使用 Youtube 数据 API,它会返回 XML/Json,您可以对其进行解析以检索视频 url 等内容。

    更新 (2017/01/24) (v3)

    使用以下调用通过搜索查询搜索 YouTube 视频:

    https://www.googleapis.com/youtube/v3/search?part=snippet&q=fun%20video&key=YOUR-API-KEY
    

    支持以下基本搜索参数:

    • part :您要在搜索中检索的视频数据。对于基本搜索,推荐值为 sn-p
    • q :要搜索的文本
    • key :您的 Google Developer API 密钥。可以在应用程序的凭据页面上的Google Developer API Console 获取此密钥。确保在您的密钥所属的应用程序上启用 Youtube Data API v3。

    更多参数见Google API Documentation

    使用 Java 库

    在 Android 上,您可以使用平台上可用的标准 HTTP 请求类向 URL 发出 HTTP 请求,也可以使用Google API Java Library,如下所示:

            YouTube youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer() {
                public void initialize(HttpRequest request) throws IOException {
                }
            }).setApplicationName("YOUR-APPLICATION-NAME").build();
    
            String queryTerm = "A fun video"
    
            // Define the API request for retrieving search results.
            YouTube.Search.List search = youtube.search().list("id,snippet");
    
            search.setKey("Your-Api-Key");
            search.setQ(queryTerm);
    
            // Call the API and print first result.
            SearchListResponse searchResponse = search.execute();
            if(searchResponse.getItems().size() == 0)
            { 
               //No items found.
               return;
            }
            SearchResult firstItem = searchResponse.getItems().get(0);
    
            ResourceId rId = firstItem.getId();
            // Confirm that the result represents a video. Otherwise, the
            // item will not contain a video ID.
            if (rId.getKind().equals("youtube#video")) {
                Thumbnail thumbnail = firstItem.getSnippet().getThumbnails().getDefault();
    
                Log.d("YOUTUBE_SAMPLE","Video Id" + rId.getVideoId());
                Log.d("YOUTUBE_SAMPLE","Title: " + firstItem.getSnippet().getTitle());
                Log.d("YOUTUBE_SAMPLE","Thumbnail: " + thumbnail.getUrl());
            }
    

    【讨论】:

    • 不再可用
    • 我已经用新的 Youtube 搜索 API 调用更新了答案
    【解决方案2】:

    大家好,感谢你们为我指明了我想要休闲的方式。我终于想出了一些东西,也想分享我的过期

    根据 youtube,我们可以请求 xml 或 json 格式的数据。我使用 json 方法来实现

    http://gdata.youtube.com/feeds/api/videos?q=title_you_want_to_search&max-results=1&v=2&alt=jsonc

    您可以从youtube developer guide获取更多信息

    上述请求“title_you_want_to_search”是您要搜索的关键字。我们可以通过将额外的参数传递给 url 来自定义结果。

    • "max-results" :提及您想要获得多少个结果(在我的情况下 我只想要一个)
    • "alt" : 你想要的结果格式为 Json 或 xml

    首先我们需要从 Youtube api 请求数据,然后我们必须从数组中选择我们想要选择的信息的哪一部分。就我而言,我使用“数据”和“项目”来获取视频 ID。在我们添加 videoId 之后,我们可以制作这样的视频 URL

    String mVideoLink = "https://youtu.be/"+videoID; (我使用以下函数来完成这件事)

    public String readYoutubeFeed(String songTitle) {
    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    String url = "http://gdata.youtube.com/feeds/api/videos?q="+songTitle+"&max-results=1&v=2&alt=jsonc";
    try {
        URLEncoder.encode(url, "UTF-8");
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
        Log.v(TAG,"encode error");
      }
     HttpGet httpGet = new HttpGet(url);        
        try {
          HttpResponse response = client.execute(httpGet);
          StatusLine statusLine = response.getStatusLine();
          int statusCode = statusLine.getStatusCode();
          if (statusCode == 200) {
             HttpEntity entity = response.getEntity();
             InputStream content = entity.getContent();
             BufferedReader reader = new BufferedReader(new InputStreamReader(content, "UTF-8"));
            String line;
            while ((line = reader.readLine()) != null) {
              builder.append(line);
            }
          } else {
            Log.v(TAG,"Failed to download file");
          }
        } catch (ClientProtocolException e) {
          e.printStackTrace();
          Log.v(TAG,"readYoutubeFeed exeption1");
        } catch (IOException e) {
          e.printStackTrace();
          Log.v(TAG,"readYoutubeFeed exeption2");
        }
        return builder.toString();
      }
    
    public String getYouTubeVideoId(String songTitle){
    String jesonData = readYoutubeFeed(songTitle);
    Log.i(TAG,jesonData);
    String title = "123";        
    try {       
        SONObject jObj = new JSONObject(jesonData); 
        JSONArray ja = jObj.getJSONObject("data").getJSONArray("items");
        JSONObject jo = (JSONObject) ja.get(0);
        title = jo.getString("id");              
        Log.v(TAG,"id is " +title);
    
    } catch (Exception e) {
        e.printStackTrace();
        Log.v(TAG,"error occerd");
      }
    return title;
    

    }

    在将字符串转换为“UTF-8”时要提到的一件重要事情是要做,因为创建 JsonArray 可能会引发异常。 可能有更好的方法来做到这一点。如果有什么建议

    【讨论】:

    • 只是想说声谢谢,你的代码sn-p为我提供了很好的参考。
    • 它不再可用的 api
    【解决方案3】:

    你应该寻找官方的 Youtube API:

    https://developers.google.com/youtube/code?hl=fr#Java

    返回你只需要解析的 Json。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-12
      相关资源
      最近更新 更多