如何在Android的ExoPlayer中播放YouTubevideo?

我试图在exoplayer播放youtubevideo,但这里有一些混乱,我不知道什么是DASHurl,我只有像“ https://www.youtube.com/watch?v=v1uyQZNg2vE ”真正的YouTubeurl,我不知道如何生成真正的url破折号的URL。

Dashurl:

new Sample("Google Glass", "http://www.youtube.com/api/manifest/dash/id/bf5bb2419360daf1/source/youtube?" + "as=fmp4_audio_clear,fmp4_sd_hd_clear&sparams=ip,ipbits,expire,as&ip=0.0.0.0&" + "ipbits=0&expire=19000000000&signature=255F6B3C07C753C88708C07EA31B7A1A10703C8D." + "2D6A28B21F921D0B245CDCF36F7EB54A2B5ABFC2&key=ik0", DemoUtil.TYPE_DASH), 

真实的url:

  https://www.youtube.com/watch?v=v1uyQZNg2vE 

Solutions Collecting From Web of "如何在Android的ExoPlayer中播放YouTubevideo?"

我已经编写了一个类,使用http://www.youtube.com/get_video_info?&video_id=%5Bvideo_id%5D&el=info&ps=default&eur==&gl=US&hl=zh_CNurl来检索实际的YouTubevideostreamurl,例如DASH和HLS格式videoID由Karim Abdell Salam描述。 我也testing了使用ExoPlayer的应用程序的URL,它的工作原理:

 import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.URL; import java.net.URLDecoder; import java.util.Map; import java.util.TreeMap; /** * Represents youtube video information retriever. */ public class YouTubeVideoInfoRetriever { private static final String URL_YOUTUBE_GET_VIDEO_INFO = "http://www.youtube.com/get_video_info?&video_id="; public static final String KEY_DASH_VIDEO = "dashmpd"; public static final String KEY_HLS_VIDEO = "hlsvp"; private TreeMap<String, String> kvpList = new TreeMap<>(); public void retrieve(String videoId) throws IOException { String targetUrl = URL_YOUTUBE_GET_VIDEO_INFO + videoId+"&el=info&ps=default&eurl=&gl=US&hl=en"; SimpleHttpClient client = new SimpleHttpClient(); String output = client.execute(targetUrl, SimpleHttpClient.HTTP_GET, SimpleHttpClient.DEFAULT_TIMEOUT); parse(output); } public String getInfo(String key) { return kvpList.get(key); } public void printAll() { System.out.println("TOTAL VARIABLES=" + kvpList.size()); for(Map.Entry<String, String> entry : kvpList.entrySet()) { System.out.print( "" + entry.getKey() + "="); System.out.println("" + entry.getValue() + ""); } } private void parse(String data) throws UnsupportedEncodingException { String[] splits = data.split("&"); String kvpStr = ""; if(splits.length < 1) { return; } kvpList.clear(); for(int i = 0; i < splits.length; ++i) { kvpStr = splits[i]; try { // Data is encoded multiple times kvpStr = URLDecoder.decode(kvpStr, SimpleHttpClient.ENCODING_UTF_8); kvpStr = URLDecoder.decode(kvpStr, SimpleHttpClient.ENCODING_UTF_8); String[] kvpSplits = kvpStr.split("=", 2); if(kvpSplits.length == 2) { kvpList.put(kvpSplits[0], kvpSplits[1]); } else if(kvpSplits.length == 1) { kvpList.put(kvpSplits[0], ""); } } catch (UnsupportedEncodingException ex) { throw ex; } } } public static class SimpleHttpClient { public static final String ENCODING_UTF_8 = "UTF-8"; public static final int DEFAULT_TIMEOUT = 10000; public static final String HTTP_GET = "GET"; public String execute(String urlStr, String httpMethod, int timeout) throws IOException { URL url = null; HttpURLConnection conn = null; InputStream inStream = null; OutputStream outStream = null; String response = null; try { url = new URL(urlStr); conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(timeout); conn.setRequestMethod(httpMethod); inStream = new BufferedInputStream(conn.getInputStream()); response = getInput(inStream); } finally { if(conn != null && conn.getErrorStream() != null) { String errorResponse = " : "; errorResponse = errorResponse + getInput(conn.getErrorStream()); response = response + errorResponse; } if (conn != null) { conn.disconnect(); } } return response; } private String getInput(InputStream in) throws IOException { StringBuilder sb = new StringBuilder(8192); byte[] b = new byte[1024]; int bytesRead = 0; while (true) { bytesRead = in.read(b); if (bytesRead < 0) { break; } String s = new String(b, 0, bytesRead, ENCODING_UTF_8); sb.append(s); } return sb.toString(); } } } 

这里是testing代码:

 public static void main(String[] args) { String youTubeVideoID = "v1uyQZNg2vE"; YouTubeVideoInfoRetriever retriever = new YouTubeVideoInfoRetriever(); try { retriever.retrieve(youTubeVideoID); System.out.println(retriever.getInfo(YouTubeVideoInfoRetriever.KEY_DASH_VIDEO)); } catch (IOException e) { e.printStackTrace(); } } 

我有同样的问题,但我终于find最简单的解决scheme,它的工作如此之好

  1. 首先你需要调用这个url

    HTTP GET: https : //www.youtube.com/get_video_info?& video_id =[ video_id] & el = info & ps = default & eurl =& gl = US &hl=en

不要忘了改变目标的最后一个ID。

  1. 现在你会得到通知下载一个名为get_video_info的文件,没有任何extesion。
  2. 尝试用记事本打开这个文件等等。
  3. 现在你有正确的数据,但你不能阅读它,因为它的编码你需要HTML解码器来使用这个数据这个数据: http : //meyerweb.com/eric/tools/dencoder/

只要粘贴你的数据,并按解码几次,以确保解码好

最后search一个名为dashmpd的密钥,并享受你的URL

你将不得不从YouTube的URL(在你的情况下真实的URL)的HTTP响应,然后search“url_encoded_fmt_stream_map”部分。 在这一节中,您将得到一个需要解码两次的URI,以获得您正在查找的DASH URL。