這個直播的視頻格式可能用的少,之前遇到了一次,現(xiàn)在想起來記錄一下
<1>根據(jù)后臺給出的m3u8的地址,實現(xiàn)播放,核心代碼貼出
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); //設(shè)置顯示視頻顯示在SurfaceView上 自己寫個SurfaceHolder設(shè)置進去,不懂的可以上網(wǎng)查下這個如何設(shè)置
} catch (IOException e) {
e.printStackTrace();
}
<2>下載m3u8的視頻缘圈。用瀏覽器打開m3u8的地址,會發(fā)現(xiàn)它是個下載地址,默認會把m3u8文件下載下來葡兑,右鍵以文本方式打開m3u8,會發(fā)現(xiàn)是一串有規(guī)律的文本文件:
#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的文件,內(nèi)容如下:
#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),這就是真正的視頻地址轩娶。
通過網(wǎng)絡(luò)下載把所有的ts文件都下載下來儿奶,我寫了個方法,在此貼出:
/**
* 給類提供訪問網(wǎng)絡(luò)的方法
* @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對應(yīng)的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對應(yīng)的字符串對象
* @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<String> m3u8List = null;//m3u8地址集合
List<String> 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<m3u8List.size();i++){
checkURlBTS(m3u8List.get(i));//使用遞歸方式
}
}else if(tsList.size()!=0){
//下載.ts文件
addLoad(tsList);
}
}
/**
*添加下載的ts文件
*/
public static void addLoad(final List<String> urlList){
//for (int i=0;i<urlList.size();i++){
for (int i=0;i<5;i++){
Log.e("get(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地址替換網(wǎng)絡(luò)的播放地址就可以了贤重。
這有篇挺好的文章,(http://www.cnblogs.com/Leo_wl/p/5646916.html)
轉(zhuǎn)載聲明出處,原文:https://blog.csdn.net/lxy_tap/article/details/52487455