先看看效果图吧,再看代码

转换文件的编码格式

 package com.xm;

 import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader; /**
* 转换文件的编码格式
*
* @author yangchuxi
*
*/
public class ConvertFileCode {
public String converfile(String filepath) {
File file = new File(filepath);
FileInputStream fis = null;
BufferedInputStream bis = null;
BufferedReader reader = null;
String text = "";
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
bis.mark(4);
byte[] first3bytes = new byte[3];
// System.out.println("");
// 找到文档的前三个字节并自动判断文档类型。
bis.read(first3bytes);
bis.reset();
if (first3bytes[0] == (byte) 0xEF && first3bytes[1] == (byte) 0xBB && first3bytes[2] == (byte) 0xBF) {// utf-8 reader = new BufferedReader(new InputStreamReader(bis, "utf-8")); } else if (first3bytes[0] == (byte) 0xFF && first3bytes[1] == (byte) 0xFE) { reader = new BufferedReader(new InputStreamReader(bis, "unicode"));
} else if (first3bytes[0] == (byte) 0xFE && first3bytes[1] == (byte) 0xFF) { reader = new BufferedReader(new InputStreamReader(bis, "utf-16be"));
} else if (first3bytes[0] == (byte) 0xFF && first3bytes[1] == (byte) 0xFF) { reader = new BufferedReader(new InputStreamReader(bis, "utf-16le"));
} else { reader = new BufferedReader(new InputStreamReader(bis, "GBK"));
}
String str = reader.readLine(); while (str != null) {
// text = text + str + "/n";
// str = reader.readLine();
text = text + str + "/n";
str = reader.readLine();
if(str==null){
break;
}
System.out.println(str);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return text;
}
}

代码

读取歌词文件

 package com.xm;
/**
* 读取歌词文件
*/
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class LrcHandle {
@SuppressWarnings("unchecked")
private List mWords = new ArrayList(); @SuppressWarnings("unchecked")
private List mTimeList = new ArrayList(); //处理歌词文件
@SuppressWarnings("unchecked")
public void readLRC(String path) {
ConvertFileCode c=new ConvertFileCode();
String a =c.converfile(path);
String[] lists = a.split("\\s"+"\n"+"|/n");
if(mWords!=null){
for(String s:lists){
addTimeToList(s);
if ((s.indexOf("[ar:") != -1) || (s.indexOf("[ti:") != -1)
|| (s.indexOf("[by:") != -1 || (s.indexOf("[offset:") != -1))) {
continue;
} else {
String ss = s.substring(s.indexOf("["), s.indexOf("]") + 1);
s = s.replace(ss, "");
}
mWords.add(s);
}
}else{
mWords.add("没有读取到歌词");
} // ConvertFileCode c=new ConvertFileCode();
// String a=c.converfile("/sdcard/xn.lrc");
// File file = new File(path);
// try {
// FileInputStream fileInputStream = new FileInputStream(file);
// InputStreamReader inputStreamReader = new InputStreamReader(
// fileInputStream);
// BufferedReader bufferedReader = new BufferedReader(
// inputStreamReader);
// String s = "";
// while ((s = bufferedReader.readLine()) != null) {
// addTimeToList(s);
// if ((s.indexOf("[ar:") != -1) || (s.indexOf("[ti:") != -1)
// || (s.indexOf("[by:") != -1)) {
// s = s.substring(s.indexOf(":") + 1, s.indexOf("]"));
// } else {
// String ss = s.substring(s.indexOf("["), s.indexOf("]") + 1);
// s = s.replace(ss, "");
// }
// mWords.add(s);
// }
// bufferedReader.close();
// inputStreamReader.close();
// fileInputStream.close();
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// mWords.add("没有歌词,快去下载");
// } catch (IOException e) {
// e.printStackTrace();
// mWords.add("没有读取到歌词");
// }
}
@SuppressWarnings("unchecked")
public List getWords() {
return mWords;
} @SuppressWarnings("unchecked")
public List getTime() {
return mTimeList;
} // 分离出时间
private int timeHandler(String string) {
string = string.replace(".", ":");
String timeData[] = string.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;
} @SuppressWarnings({ "unchecked", "unused" })
private void addTimeToList(String string) {
Matcher matcher = Pattern.compile(
"\\[\\d{1,2}:\\d{1,2}([\\.:]\\d{1,2})?\\]").matcher(string);
if (matcher.find()) {
String str = matcher.group();
mTimeList.add(new LrcHandle().timeHandler(str.substring(1,
str.length() - 1)));
}
}
}

代码

实现xml

 package com.xm;

 import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView; public class WordView extends TextView {
@SuppressWarnings("unchecked")
private List mWordsList = new ArrayList();
private Paint mLoseFocusPaint;
private Paint mOnFocusePaint;
private float mX = 0;
private float mMiddleY = 0;
private float mY = 0;
private static final int DY = 50;
private int mIndex = 0;
private String name; public WordView(Context context) throws IOException {
super(context);
init();
} public WordView(Context context, AttributeSet attrs) throws IOException {
super(context, attrs);
init();
} public WordView(Context context, AttributeSet attrs, int defStyle)throws IOException {
super(context, attrs, defStyle);
init();
} @Override
protected void onDraw(Canvas canvas) {
if(mWordsList.size()==0){
LrcHandle lrcHandler = new LrcHandle();
lrcHandler.readLRC("/sdcard/"+name);
mWordsList = lrcHandler.getWords();
}
super.onDraw(canvas);
canvas.drawColor(Color.BLACK);
Paint p = mLoseFocusPaint;
p.setTextAlign(Paint.Align.CENTER);
Paint p2 = mOnFocusePaint;
p2.setTextAlign(Paint.Align.CENTER);
canvas.drawText((String) mWordsList.get(mIndex), mX, mMiddleY, p2);
int alphaValue = 25;
float tempY = mMiddleY;
for (int i = mIndex - 1; i >= 0; i--) {
tempY -= DY;
if (tempY < 0) {
break;
}
p.setColor(Color.argb(255 - alphaValue, 245, 245, 245));
canvas.drawText((String) mWordsList.get(i), mX, tempY, p);
alphaValue += 25;
}
alphaValue = 25;
tempY = mMiddleY;
for (int i = mIndex + 1, len = mWordsList.size(); i < len; i++) {
tempY += DY;
if (tempY > mY) {
break;
}
p.setColor(Color.argb(255 - alphaValue, 245, 245, 245));
canvas.drawText((String) mWordsList.get(i), mX, tempY, p);
alphaValue += 25;
}
mIndex++;
} @Override
protected void onSizeChanged(int w, int h, int ow, int oh) {
super.onSizeChanged(w, h, ow, oh);
mX = w * 0.5f;
mY = h;
mMiddleY = h * 0.3f;
} // @SuppressLint("SdCardPath")
private void init() throws IOException {
setFocusable(true);
// LrcHandle lrcHandler = new LrcHandle();
// lrcHandler.readLRC("/sdcard/wwyx.lrc");
// mWordsList = lrcHandler.getWords(); mLoseFocusPaint = new Paint();
mLoseFocusPaint.setAntiAlias(true);
mLoseFocusPaint.setTextSize(22);
mLoseFocusPaint.setColor(Color.WHITE);
mLoseFocusPaint.setTypeface(Typeface.SERIF); mOnFocusePaint = new Paint();
mOnFocusePaint.setAntiAlias(true);
mOnFocusePaint.setColor(Color.YELLOW);
mOnFocusePaint.setTextSize(30);
mOnFocusePaint.setTypeface(Typeface.SANS_SERIF);
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}

代码

xml配置文件

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
>
<TableLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
>
<TableRow>
<ImageButton
android:id="@+id/ibgc1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/hh"
android:background="#00000000"
android:layout_marginLeft="10px"
android:layout_marginRight="98px"
/>
</TableRow>
</TableLayout>
<TableLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TableRow>
<com.xm.WordView
android:id="@+id/text"
android:layout_width="330px"
android:layout_height="355px"
/>
</TableRow>
</TableLayout>
<TableLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:layout_alignParentBottom="true"
android:layout_marginTop="10px"
>
<TableRow>
<Button
android:id="@+id/bt10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始"
android:background="#FFFFFF"
android:layout_marginLeft="30px"
/>
<Button
android:id="@+id/bt11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停"
android:background="#FFFFFF"
android:layout_marginLeft="180px"
/>
</TableRow>
</TableLayout>
</LinearLayout>

xml

Activity类

 private WordView mWordView;
private List mTimeList;
private MediaPlayer mPlayer;
private boolean isPause;
private boolean isStartTrackingTouch;
final Handler handler = new Handler();
bt10 = (Button) findViewById(R.id.bt10);
bt10.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mPlayer.start();
isPause = false;
new Thread(new Runnable() {
int i = 0;
@Override
public void run() {
while (mPlayer.isPlaying()) {
handler.post(new Runnable() {
@Override
public void run() {
mWordView.invalidate();
}
});
try {
int a = Integer.parseInt(String.valueOf(mTimeList
.get(i + 1)));
int b = Integer.parseInt(String.valueOf(mTimeList
.get(i)));
Thread.sleep(a - b);
} catch (Exception e) {
}
i++;
if (i == mTimeList.size() - 1) {
mPlayer.stop();
break;
}
}
}
}).start();
}
});
bt11 = (Button) findViewById(R.id.bt11);
bt11.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mPlayer.pause();
isPause = true;
}
});
LrcHandle lrcHandler = new LrcHandle();
String name1 = getIntent().getStringExtra("name");
String str=name1.substring(0,name1.indexOf('.'));
String str1=str+".lrc";
String name2 = getIntent().getStringExtra("name");
mWordView = (WordView) findViewById(R.id.text);
mWordView.setName(str1);
mPlayer = new MediaPlayer();
mPlayer.reset();
try {
lrcHandler.readLRC("/sdcard/"+str1);
mTimeList = lrcHandler.getTime();
mPlayer.setDataSource("/sdcard/"+name2);
mPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}

代码

最新文章

  1. AngularJs最简单解决跨域问题案例
  2. ASP.NET中处理自定义错误的最佳方式
  3. css中的zoom的使用
  4. 【HDOJ】4080 Stammering Aliens
  5. JS判断移动设备最佳方法
  6. DB2 insert into 三种写法
  7. 怎样查询SCI和EI检索号
  8. transition的唧唧歪歪
  9. mysql监控、性能调优及三范式理解
  10. Spring的ApplicationEvent实现
  11. cocoapod Podfile use frameworks swift/oc混编 could not build module xxx
  12. 朴素贝叶斯分类算法介绍及python代码实现案例
  13. Qt5使用QFtp,二次封装
  14. 公共的service接口
  15. matplotlib交互模式与pacharm单独Figure设置
  16. css笔记 - 张鑫旭css课程笔记之 z-index 篇
  17. 使用redis的发布订阅模式实现消息队列
  18. javascript飞机大战-----005创建子弹对象2
  19. ehcache 的 配置文件: ehcache.xml的认识
  20. program发展史及以后预测

热门文章

  1. cas添加验证码
  2. Python(内置函数)
  3. Python高阶函数-闭包
  4. python 2 和python 3的 区别
  5. OpenFileDialog.Filter 属性
  6. angularjs 的controller的三种写法
  7. Python3:读取配置dbconfig.ini(含有中文)显示乱码的解决方法
  8. 强大的jQuery幻灯片播放插件 支持全拼、拖拽和下载等功能
  9. django使用migrations迁移版本和数据库中报错解决方案
  10. 利用web workers实现多线程处理