注意:原问题已删除原始问题是https://stackoverflow.com/questions/15240011/get-the-download-url-for-youtube-video-android-java/15240012#15240012这里的答案已经过时,不起作用,所以我会发布一个新问题并回答我自己旧代码new YouTubePageStreamUriGetter().execute("https://www.youtube.com/watch?v=4GuqB1BQVr4");class Meta { public String num; public String type; public String ext; Meta(String num, String ext, String type) { this.num = num; this.ext = ext; this.type = type; }}class Video { public String ext = ""; public String type = ""; public String url = ""; Video(String ext, String type, String url) { this.ext = ext; this.type = type; this.url = url; }}public ArrayList<Video> getStreamingUrisFromYouTubePage(String ytUrl) throws IOException { if (ytUrl == null) { return null; } // Remove any query params in query string after the watch?v=<vid> in // e.g. // http://www.youtube.com/watch?v=0RUPACpf8Vs&feature=youtube_gdata_player int andIdx = ytUrl.indexOf('&'); if (andIdx >= 0) { ytUrl = ytUrl.substring(0, andIdx); } // Get the HTML response String userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0.1)"; HttpClient client = new DefaultHttpClient(); client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, userAgent); HttpGet request = new HttpGet(ytUrl); HttpResponse response = client.execute(request); String html = ""; InputStream in = response.getEntity().getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder str = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { str.append(line.replace("\\u0026", "&")); } in.close(); html = str.toString(); // Parse the HTML response and extract the streaming URIs if (html.contains("verify-age-thumb")) { CLog.w("YouTube is asking for age verification. We can't handle that sorry."); return null; }此代码不起作用
添加回答
举报
0/150
提交
取消