if(files[i].getName().endsWith("mp3")){
Mp3Infomp3Info=newMp3Info();
mp3Info.setMp3Name(files[i].getName());
mp3Info.setMp3Size(files[i].length()+"");
Stringlrcname[]=files[i].getName().split("\\.");
Stringlrc=lrcname[0]+".lrc";
if(isFileExist(lrc,"/mp3")){
mp3Info.setLrcName(lrc);
}
mp3Infos.add(mp3Info);
}
}
returnmp3Infos;
}
}
HttpDownloader.java
packagemars.download;
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
import.HttpURLConnection;
import.MalformedURLException;
import.URL;
publicclassHttpDownloader{
privateURLurl=null;
/**
*根据URL下载文件,前提是这个文件是文本的,函数的返回值就是文件当中的内容
*1.创建一个URL对象
*2.通过URL对象,创建一个HttpURLConnection对象
*3.得到InputStream
*4.从InputStream得到数据
*/
publicStringdownload(StringurlStr){
StringBuffersb=newStringBuffer();
Stringline=null;
BufferedReaderbuffer=null;
try{
//创建一个url连接
url=newURL(urlStr);
//创建一个http连接
HttpURLConnectionurlConn=(HttpURLConnection)url.openConnection();
//使用IO读取数据
buffer=newBufferedReader(newInputStreamReader(urlConn.getInputStream()));
while((line=buffer.readLine())!
=null){
sb.append(line);
}
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
buffer.close();
}catch(Exceptione){
e.printStackTrace();
}
}
returnsb.toString();
}
/**
*该函数返回整形,-1代表下载文件出错,0代表下载文件成功,1代表文件已经存在
*/
publicintdownFile(StringurlStr,Stringpath,StringfileName){
InputStreaminputStream=null;
try{
FileUtilsfu=newFileUtils();
if(fu.isFileExist(fileName,path)){
return1;
}else{
inputStream=getInputStreamFromUrl(urlStr);
System.out.println("test");
FileresultFile=fu.write2SDFromInput(path,fileName,inputStream);
if(resultFile==null){
return-1;
}
}
}catch(Exceptione){
e.printStackTrace();
}
return0;
}
/**
*根据URL得到输入流
*/
publicInputStreamgetInputStreamFromUrl(StringurlStr)throwsMalformedURLException,IOException{
url=newURL(urlStr);
HttpURLConnectionurlConn=(HttpURLConnection)url.openConnection();
InputStreaminputStream=urlConn.getInputStream();
returninputStream;
}
}
LrcProcesor.java
packagemars.lrc;
importjava.io.BufferedReader;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.util.ArrayList;
importjava.util.LinkedList;
importjava.util.Queue;
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
publicclassLrcProcesor{
@SuppressWarnings("rawtypes")
publicArrayListprocess(InputStreaminputStream){
//存放时间点数据
QueuetimeMills=newLinkedList();
//存放时间点对应的歌词
Queuemessages=newLinkedList();
//将放时间和歌词存放到一起
ArrayListqueues=newArrayList();
try{
//创建BufferedReader对象
InputStreamReaderinputReader=newInputStreamReader(inputStream,"GBK");
BufferedReaderbufferedReader=newBufferedReader(inputReader);
Stringtemp=null;
@SuppressWarnings("unused")
inti=0;
//创建一个正则表达式
Patternp=Ppile("\\[([^\\]]+)\\]");
Stringresult=null;
booleanb=true;
while((temp=bufferedReader.readLine())!
=null){
i++;
Matcherm=p.matcher(temp);
if(m.find()){
if(result!
=null){
messages.add(result);
}
StringtimeStr=m.group();
LongtimeMill=time2Long(timeStr.substring(1,timeStr.length()-1));
if(b){
timeMills.offer(timeMill);
}
Stringmsg=temp.substring(10);
result=""+msg+"\n";
}else{
result=result+temp+"\n";
}
}
messages.add(result);
queues.add(timeMills);
queues.add(messages);
}catch(Exceptione){
e.printStackTrace();
}
returnqueues;
}
/**
*将分钟,秒全部转化为毫秒
*@paramtimeStr
*@return
*/
publicLongtime2Long(StringtimeStr){
Strings[]=timeStr.split(":
");
intmin=Integer.parseInt(s[0]);
Stringss[]=s[1].split("\\.");
intsec=Integer.parseInt(ss[0]);
intmill=Integer.parseInt(ss[1]);
returnmin*60*1000+sec*1000+mill*10L;
}
}
Mp3Info.java
packagemars.model;
importjava.io.Serializable;
publicclassMp3InfoimplementsSerializable{
/**
*
*/
privatestaticfinallongserialVersionUID=1L;
privateStringid;
privateStringmp3Name;
privateStringmp3Size;
privateStringlrcName;
privateStringlrcSize;
publicStringgetId(){
returnid;
}
publicvoidsetId(Stringid){
this.id=id;
}
publicStringgetMp3Name(){
returnmp3Name;
}
publicvoidsetMp3Name(Stringmp3Name){
this.mp3Name=mp3Name;
}
publicMp3Info(){
super();
}
publicStringgetMp3Size(){
returnmp3Size;
}
publicvoidsetMp3Size(Stringmp3Size){
this.mp3Size=mp3Size;
}
publicStringgetLrcName(){
returnlrcName;
}
publicvoidsetLrcName(StringlrcName){
this.lrcName=lrcName;
}
publicStringgetLrcSize(){
returnlrcSize;
}
publicvoidsetLrcSize(StringlrcSize){
this.lrcSize=lrcSize;
}
@Override
publicStringtoString(){
return"Mp3Info[id="+id+",mp3Name="+mp3Name+",mp3Size="
+mp3Size+",lrcName="+lrcName+",lrcSize="+lrcSize
+",toString()="+super.toString()+"]";
}
publicMp3Info(Stringid,Stringmp3Name,Stringmp3Size,StringlrcName,
StringlrcSize){
super();
this.id=id;
this.mp3Name=mp3Name;
this.mp3Size=mp3Size;
this.lrcName=lrcName;
this.lrcSize=lrcSize;
}
}
AppConstant.java
packagemars.mp3player;
publicinterface