我试图在exoplayer中播放youtube视频,但这里有一些混乱我不知道什么是DASH url,
我只有真正的youtube网址,如“https://www.youtube.com/watch?v=v1uyQZNg2vE”,我不知道如何生成破折号网址形式真实的网址.
短划线网址:
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),
真实网址:
https://www.youtube.com/watch?v=v1uyQZNg2vE
解决方法:
我已经编写了一个类,根据Karim Abdell Salam所描述的,使用带有视频ID的 http://www.youtube.com/get_video_info?&video_id=[video_id]&el=info&ps=default&eurl=&gl=US&hl=en网址检索实际的YouTube视频流网址,例如DASH和HLS.我还在使用ExoPlayer的应用中测试了网址,它的工作原理如下:
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(); } }}
这是测试代码:
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(); }}
总结 以上是内存溢出为你收集整理的如何在Android中的ExoPlayer中播放Youtube视频?全部内容,希望文章能够帮你解决如何在Android中的ExoPlayer中播放Youtube视频?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)