这个直播的视频格式可能用的少,之前遇到了一次,现在想起来记录一下
<1>根据后台给出的m3u8的地址,实现播放,核心代码贴出
try {
String url = "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8";//测试的m3u8地址
//String url="http://devimages.apple.com/iphone/samples/bipbop/gear1/fileSequence0.ts";
MediaPlayer mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource(url);
mPlayer.prepare(); // might take long! (for buffering, etc)
mPlayer.setDisplay(surfaceHolder); //设置显示视频显示在SurfaceView上 自己写个SurfaceHolder设置进去,不懂的可以上网查下这个如何设置
} catch (IOException e) {
e.printStackTrace();
}
<2>下载m3u8的视频。用浏览器打开m3u8的地址,会发现它是个下载地址,默认会把m3u8文件下载下来,右键以文本方式打开m3u8,会发现是一串有规律的文本文件:
#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=200000
gear1/prog_index.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=311111
gear2/prog_index.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=484444
gear3/prog_index.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=737777
gear4/prog_index.m3u8
m3u8的地址你都不能直接得到,我们用得到这个文件的链接地址的前半段(http://devimages.apple.com/iphone/samples/bipbop/)拼接上二级文件的相对地址gear1/prog_index.m3u8得到一个地址(http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8)
把此地址放到浏览器中,我们又会得到一个同样名为prog_index.m3u8的文件,内容如下:
#EXTM3U
#EXT-X-TARGETDURATION:10
#EXT-X-MEDIA-SEQUENCE:0
#EXTINF:10, no desc
fileSequence0.ts
#EXTINF:10, no desc
fileSequence1.ts
#EXTINF:10, no desc
fileSequence2.ts
#EXTINF:10, no desc
fileSequence3.ts
#EXTINF:10, no desc
fileSequence4.ts
#EXTINF:10, no desc
////省略后半部分
<3>终于可以看到ts文件名了,我们还是没有得到真正的视频地址,老办法拼接后我们得到这么一段链(http://devimages.apple.com/iphone/samples/bipbop/gear1/fileSequence179.ts),这就是真正的视频地址。
通过网络下载把所有的ts文件都下载下来,我写了个方法,在此贴出:
/**
* 给类提供访问网络的方法
* @author Administrator
*
*/
public final class HttpConnect {
/*import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;*/
/**
* 利用HttpClient获取指定的Url对应的HttpResponse对象
* @param url
* @return
*/
public static HttpResponse getResponseFromUrl(String url){
try {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse response = client.execute(get);
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
return response;
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
/**
* 利用HttpClient获取指定Url对应的字符串对象
* @param url
* @return
*/
public static String getStringFromUrl(String url){
try {
StringBuilder result = new StringBuilder();
HttpResponse res = HttpConnect.getResponseFromUrl(url);
if(res != null){
InputStream is = res.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = "";
while((line = reader.readLine()) != null){
result.append(line);
}
is.close();
return result.toString();
}
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
}
使用:
/**
* 解析m3u8,下载m3u8文件
* @param url 传入m3u8的地址
*/
public static void checkURlBTS(String url) {
List m3u8List = null;//m3u8地址集合
List tsList = null;//ts文件地址集合额
HttpResponse res = HttpConnect.getResponseFromUrl(url);
//HttpResponse res = HttpConnect.getResponseFromUrl(url);
try {
if (res != null) {
m3u8List = new ArrayList<>();
tsList=new ArrayList<>();
InputStream in = res.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null) {
if (line.startsWith("#")) {
} else if (line.length() > 0 && line.endsWith(".m3u8")) {
String mUpM3u8URL=url.substring(0,url.lastIndexOf("/")+1);
m3u8List.add(mUpM3u8URL+line);
} else if (line.length() > 0 && line.endsWith(".ts")) {//判断该文件是否含有.ts文件
String mUpTSURL=url.substring(0,url.lastIndexOf("/")+1);
tsList.add(mUpTSURL+line);
}
}
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}
if(m3u8List!=null&&m3u8List.size()!=0){
for (int i=0;i urlList){
//for (int i=0;i",""+urlList.get(i));
load(urlList.get(i));
}
}
/**
* @Desciption: 读取任意文件,并将文件保存到手机SDCard
*/
public static void load(String loadURL) {
String path = loadURL.substring(loadURL.substring(0, loadURL.lastIndexOf("/")).lastIndexOf("/") + 1, loadURL.lastIndexOf("/"));
String fileName = loadURL.substring(loadURL.lastIndexOf("/") + 1, loadURL.length());
OutputStream output = null;
try {
//因ts文件非常之多,下载很缓慢,我们项目是使用了okHttp下载的,但为了方便大家,写了个httpclient下载
URL url = new URL(loadURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//取得inputStream,并将流中的信息写入SDCard
String SDCard = Environment.getExternalStorageDirectory() + "";
String pathName = SDCard + "/" + path + "/" + fileName;//文件存储路径
File file = new File(pathName);
InputStream input = conn.getInputStream();
if (file.exists()) {
return;
} else {
String dir = SDCard + "/" + path;
new File(dir).mkdir();//新建文件夹
file.createNewFile();//新建文件
output = new FileOutputStream(file);
//读取大文件
byte[] buffer = new byte[4 * 1024];
while (input.read(buffer) != -1) {
output.write(buffer);
}
output.flush();
Log.e("success-->","come in");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (output!=null){
output.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
最后播放缓存的m3u8.这个就不贴代码了,播放时,就把本地的m3u8地址替换网络的播放地址就可以了。
这有篇挺好的文章,(http://www.cnblogs.com/Leo_wl/p/5646916.html)
转载声明出处,原文:https://blog.csdn.net/lxy_tap/article/details/52487455