-
Notifications
You must be signed in to change notification settings - Fork 109
Open
Description
修改MUtils文件parseIndex()方法
public static M3U8 parseIndex(String url) throws IOException {
String urlName = url;
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
String newUrl = urlName;
Map<String, List<String>> map = conn.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key).get(0));
if ("Location".equals(key)) {
//获取新地址
newUrl = map.get(key).get(0);
System.out.println("newUrl--->" +newUrl);
break;
}
}
// 重新实例化url对象
realUrl = new URL(newUrl);
// 重新打开和URL之间的连接
conn = realUrl.openConnection();
// 建立实际的连接
conn.connect();
InputStream in = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in,encoding));
String basepath = url;
if (url.contains("?")) {
basepath = url.substring(0, url.lastIndexOf("?"));
}
basepath = basepath.substring(0, basepath.lastIndexOf("/") + 1);
M3U8 ret = new M3U8();
ret.setBasePath(basepath);
String line;
float seconds = 0;
while ((line = reader.readLine()) != null) {
if (line.startsWith("#")) {
if (line.startsWith("#EXTINF:")) {
line = line.substring(8);
if (line.endsWith(",")) {
line = line.substring(0, line.length() - 1);
}
seconds = Float.parseFloat(line);
}
continue;
}
if (line.endsWith("m3u8")) {
return parseIndex(basepath + line);
}
ret.addTs(new M3U8Ts(line, seconds));
seconds = 0;
}
reader.close();
return ret;
}
-
M3U8DownloadTask文件的startDownload()方法
executor.execute(new Runnable() { @Override public void run() { File file; try { String fileName = M3U8EncryptHelper.encryptFileName(encryptKey, m3U8Ts.obtainEncodeTsFileName()); file = new File(dir + File.separator + fileName); } catch (Exception e) { file = new File(dir + File.separator + m3U8Ts.getUrl()); } if (!file.exists()) {//下载过的就不管了 String urlName = m3U8Ts.obtainFullUrl(basePath); FileOutputStream fos = null; InputStream inputStream = null; try { URL url = new URL(m3U8Ts.obtainFullUrl(basePath)); String newUrl = urlName; HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(connTimeout); conn.setReadTimeout(readTimeout); Map<String, List<String>> map = conn.getHeaderFields(); // 遍历所有的响应头字段 for (String key : map.keySet()) { System.out.println(key + "--->" + map.get(key).get(0)); if ("Location".equals(key)) { //获取新地址 newUrl = map.get(key).get(0); System.out.println("newUrl--->" +newUrl); break; } } // 重新实例化url对象 url = new URL(newUrl); // 重新打开和URL之间的连接 conn = (HttpURLConnection) url.openConnection(); // 建立实际的连接 conn.connect(); if (conn.getResponseCode() == 200) { if (isStartDownload){ isStartDownload = false; mHandler.sendEmptyMessage(WHAT_ON_START_DOWNLOAD); } inputStream = conn.getInputStream(); fos = new FileOutputStream(file);//会自动创建文件 int len = 0; byte[] buf = new byte[8 * 1024 * 1024]; while ((len = inputStream.read(buf)) != -1) { curLength += len; fos.write(buf, 0, len);//写入流中 } } else { handlerError(new Throwable(String.valueOf(conn.getResponseCode()))); } } catch (MalformedURLException e) { handlerError(e); } catch (IOException e) { handlerError(e); } catch (Exception e) { handlerError(e); } finally {//关流 if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { } } if (fos != null) { try { fos.close(); } catch (IOException e) { } } } itemFileSize = file.length(); m3U8Ts.setFileSize(itemFileSize); mHandler.sendEmptyMessage(WHAT_ON_PROGRESS); curTs++; }else { curTs ++; itemFileSize = file.length(); m3U8Ts.setFileSize(itemFileSize); } } });
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels