}
JSONObjectjObject=newJSONObject(sb.toString());
intcount=jObject.getInt(“count”);
intindex=count==0?0:newRandom().nextInt()%count;
JSONArrayjArray=jObject.getJSONArray(“result”);
JSONObjectobj=jArray.getJSONObject(index);
returnobj.getString(“lrc”);
}catch(MalformedURLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(JSONExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returnnull;
}
//歌手,歌曲名中的空格进行转码
publicStringEncode(Stringstr){
try{
returnURLEncoder.encode(str.trim(),“utf-8”);
}catch(UnsupportedEncodingExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returnstr;
}
//歌词文件网络地址,歌词文件本地缓冲地址
publicbooleanwrtieContentFromUrl(StringurlPath,StringlrcPath){
Log.i(TAG,“lrcURL”+urlPath);
try{
URLurl=newURL(urlPath);
URLConnectionurlConnection=url.openConnection();
urlConnection.connect();
HttpURLConnectionhttpConn=(HttpURLConnection)urlConnection;
if(httpConn.getResponseCode()==HttpURLConnection.HTTP_OK){
Filefile=newFile(lrcRootPath);
if(!file.exists()){
file.mkdirs();
}
BufferedReaderbf=newBufferedReader(newInputStreamReader(
urlConnection.getInputStream(),“utf-8”));
PrintWriterout=newPrintWriter(newBufferedWriter(
newOutputStreamWriter(newFileOutputStream(lrcPath),
“utf-8”)));
charc[]=newchar[256];
inttemp=-1;
while((temp=bf.read())!=-1){
bf.read©;
out.write©;
}
bf.close();
out.close();
returntrue;
}
//System.out.println(“getFile:”+str);
}catch(MalformedURLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returnfalse;
}
publicStringgetLrcPath(Stringtitle,Stringartist){
returnlrcRootPath+title+"-"+artist+“.lrc”;
}
}
LrcProcess 类,用来保存处理下载后的文件
public class LrcProcess {
private List lrcList; //List集合存放歌词内容对象
private LrcContent mLrcContent; //声明一个歌词内容对象
/**
无参构造函数用来实例化对象
*/
public LrcProcess() {
mLrcContent = new LrcContent();
lrcList = new ArrayList();
}
/**
读取歌词
@param path
@return
*/
public String readLRC(String path) {
//定义一个StringBuilder对象,用来存放歌词内容
StringBuilder stringBuilder = new StringBuilder();
File f = new File(path.replace(".mp3", “.lrc”));
try {
//创建一个文件输入流对象
FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fis, “utf-8”);
BufferedReader br = new BufferedReader(isr);
String s = “”;
while((s = br.readLine()) != null) {
//替换字符
s = s.replace("[", “”);
s = s.replace("]", “@”);
//分离“@”字符
String splitLrcData[] = s.split("@");
if(splitLrcData.length > 1) {
Log.i(“INFO”, splitLrcData[1]+“歌词”);
mLrcContent.setLrcStr(splitLrcData[1]);
Log.i(“INFO”, splitLrcData[0]+“时间”);
//处理歌词取得歌曲的时间
int lrcTime = time2Str(splitLrcData[0]);
mLrcContent.setLrcTime(lrcTime);
//添加进列表数组
lrcList.add(mLrcContent);
//新创建歌词内容对象
mLrcContent = new LrcContent();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
stringBuilder.append(“木有歌词文件,赶紧去下载!…”);
} catch (IOException e) {
e.printStackTrace();
stringBuilder.append(“木有读取到歌词哦!”);
}
return stringBuilder.toString();
}
/**
解析歌词时间
歌词内容格式如下:
[00:02.32]陈奕迅
[00:03.43]好久不见
[00:05.22]歌词制作 王涛
@param timeStr
@return
*/
public int time2Str(String timeStr) {
timeStr = timeStr.replace(":", “.”);
timeStr = timeStr.replace(".", “@”);
String timeData[] = timeStr.split("@"); //将时间分隔成字符串数组
//分离出分、秒并转换为整型
int minute = Integer.parseInt(timeData[0]);
int second = Integer.parseInt(timeData[1]);
int millisecond = Integer.parseInt(timeData[2]);
//计算上一行与下一行的时间转换为毫秒数
int currentTime = (minute * 60 + second) * 1000 + millisecond * 10;
return currentTime;
}
public List getLrcList() {
return lrcList;
}
Mainactivity 大概调用方法
public class MainActivity extends Activity {
//好久不见 青花
private String mMusicName=“夜曲”;
//陈奕迅 周传雄
private String mMusicAutor=“周杰伦”;
@SuppressLint(“SdCardPath”)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(){
public void run() {
String lrcURL = OnlineLrcUtil.getLrcURL(mMusicName, mMusicAutor);
tivity {
//好久不见 青花
private String mMusicName=“夜曲”;
//陈奕迅 周传雄
private String mMusicAutor=“周杰伦”;
@SuppressLint(“SdCardPath”)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(){
public void run() {
String lrcURL = OnlineLrcUtil.getLrcURL(mMusicName, mMusicAutor);
如果觉得《android音乐播放器开发加载歌词 android自定义view面试》对你有帮助,请点赞、收藏,并留下你的观点哦!