Android开发在线视频应用-解析流媒体M3U8文件

来源:互联网 发布:淘宝投诉盗图怎么办 编辑:程序博客网 时间:2024/06/06 08:31

流媒体m3u8文件在ihpone上直接可以丢给播放器去解析播放,然而转到android端就行不通了,所以想要在android播放m3u8只能自己去实现m3u8文件的解析。

1.假设我们已经下载了一个video.m3u8文件在手机的sd卡中,现在我们要做的事情就是读到该文件并进行解析,下面的代码实现了m3u8文件的解析


/** 

解析m3u8文件获取视频播放碎片地址及播放时长等信息 

将解析出的信息放到list集合中返回给调用

filePath:m3u8文件在你sd中的绝对路径

**/
public static List<String> M3u8(String filePath){
BufferedReader br = null;
String line = null;
List<String> list = new ArrayList<String>();
try {
// 根据文件路径创建缓冲输入流
br = new BufferedReader(new FileReader(filePath));
// 循环读取文件的每一行将其 放入缓冲对象中
while ((line = br.readLine()) != null) {
// 此处根据实际需要获取某些行的内容
if (line.startsWith("http:") || line.startsWith("#EXT-X-DISCONTINUITY") || line.startsWith("#EXT-X-ENDLIST")) {
list.add(line);
}

}
} catch (Exception e) {
e.printStackTrace();
Log.e("获取失败");
} finally {
// 关闭流
if (br != null) {
try {
br.close();
} catch (IOException e) {
br = null;
}
}
}
return list;
}


2.将m3u8文件的播放地址碎片批量修复并回写,两个方法实现该步骤,分别是Filewrite和read

Filewrite方法实现回写

 /** 
     
将内容回写到文件中 
      
     */  
    public static boolean Filewrite(String filePath, String obj) {  
        BufferedWriter bw = null;  
        boolean bool = false;
        try {  
            // 根据文件路径创建缓冲输出流  
            bw = new BufferedWriter(new FileWriter(filePath));  
            // 将内容写入文件中  
            bw.write(obj);
            bool = true;
           
Log.i("回写成功");
        } catch (Exception e) {  
            e.printStackTrace();  
           
Log.e("回写失败");
        } finally {  
            // 关闭流  
            if (bw != null) {  
                try {  
                    bw.close();  
                } catch (IOException e) {  
                    bw = null;  
                }  
            }  
        }
        return bool;
    }

read方法实现将要修改的行内容读取到并进行修改

/** 
      读取文件内容 
     
  */  
    public static String read(String filePath) {  
        BufferedReader br = null;  
        String line = null;  
        StringBuffer buf = new StringBuffer();  
          int i = 0;
        try {  
            // 根据文件路径创建缓冲输入流  
            br = new BufferedReader(new FileReader(filePath));  
            // 循环读取文件的每一行, 对需要修改的行进行修改, 放入缓冲对象中  
            while ((line = br.readLine()) != null) {  
                // 此处根据实际需要修改某些行的内容  
                if (line.startsWith("http:")) {
                    buf.append("file:/"+filePath+"/"+i+".ts");  
                    i++;
                }else {  
                    buf.append(line);  
                }  
                buf.append(System.getProperty("line.separator"));  
                }  
        } catch (Exception e) {  
            e.printStackTrace(); 
           
Log.e("修改失败");
        } finally {  
            // 关闭流  
            if (br != null) {  
                try {  
                    br.close();  
                } catch (IOException e) {  
                    br = null;  
                }  
            }  
        }  
          
        return buf.toString();  
    }  


3.回写m3u8文件的调用方式

   Filewrite(绝对路径,read(绝对路径));


以上为m3u8文件的解析读取和内容回写,下篇将会更新m3u8流媒体文件下载完整视频

0 0