-
-10 17
-
android2.1以下的系统在流媒体上做的都不够好,不仅仅是软件只能装在手机里面而不能装到SD卡。解决这样的问题只能通过将要播放的流媒体下载下来在使用系统的播放器播放,我在模拟器上做的实验,2.2的支持流媒体,但是现在的机子要兼容啊!之前我在网上搜索到的流媒体的问题好像都是什么流媒体格式的问题,但是为了有更广的兼容性这个方案最好:一边下载一边播放。
设计方案:1/5的文件先下载下来,其他的采用多个线程解决,边播放的同时边下载。也可以分段下载然后再合并成一个文件,这样会快于单个文件的下载。
下载类的核心部分代码:
public class downLoad1 {
private URL myURL;
private URLConnection conn;
public InputStream is;
public File currentfile;
public static boolean isOver=false;
public int num;
public String currentfile_string;
// String s1,s2,s3,s4;
public boolean start_download(){
// try {
// s1=URLEncoder.encode("福建","GBK");
// } catch (UnsupportedEncodingException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
String strpath = "http://网址/res/video/"+s1+"/"+s2+"/"+s3+"/"+s4+"/gssbj.3gp";
try {
myURL = new URL(strpath);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn = myURL.openConnection();
conn.setDoInput(true);// 允许输入
conn.connect();
is = conn.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
//创建文件夹
File file=new File("d://sdcard/helpyo/v/");
if(!file.exists())
file.mkdirs();
//创建文件
if(strpath.endsWith(".3gp")||strpath.endsWith(".3GP")){
currentfile_string="d://sdcard/helpyo/v/mzzo_temp.3gp";
currentfile=new File("d://sdcard/helpyo/v/mzzo_temp.3gp");
}
else{
currentfile_string="d://sdcard/helpyo/v/mzzo_temp.MP4";
currentfile=new File("d://sdcard/helpyo/v/mzzo_temp.MP4");
}
if(currentfile.exists())
currentfile.delete();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(currentfile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte buf[] = new byte[9000];
int numread = 0;
//下载主入口
do
{
try {
numread = is.read(buf);
if(numread==-1)
{
isOver=true;
break;
}
fos.write(buf, 0, numread);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}while (true);
if(isOver)
{
// helpBean.setTempf(currentfile_string);
isOver=false;
return true;
}
else
return false;
}
}
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。
- 评论(0)
发表评论 TrackBack