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

转换文件的编码格式

 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();
}

代码

Android中从SD卡中获取歌词并与歌曲同步的更多相关文章

  1. Android BaseAdapter ListView (SD卡中文件目录显示出来)

    首先搭建activity_main.xml布局 搭建ListView中显示的布局 创建适配器 将File数据和UI适配 MainActivity中将ListView设置适配器,并设置监听 //获取SD ...

  2. Android中从SD卡中读取歌曲

    先看看我的效果图吧 Activity类 private TextView nameTextView; private SeekBar seekBar; private ListView listVie ...

  3. 将文件放到Android模拟器的SD卡中的两种解决方法

    两种方式:一.窗口界面操作1.打开DDMS页面2.打开File Explorer页,如果没有,在Window --> Show View -->File Explorer3.一般就在mnt ...

  4. Android模拟器使用SD卡

    在Android的应用开发中经常要用到与SD卡有关的调试,本文就是介绍关于在Android模拟器中SD卡的使用 一.      准备工作 在介绍之前首先做好准备工作,即配好android的应用开发环境 ...

  5. Android获取SD卡中选中图片的路径(URL)

    最近在做一个图片上传的功能,需要提供上传图片在SD卡中的路径,在网上看了些例子,改改调试成功,代码很简单.其布局文件如下: [html]  view plain copy   <?xml ver ...

  6. Android中使用SQLiteOpenHelper管理SD卡中的数据库

    使用Android中自带的SQLiteOpenHelper可以完成数据库的创建与管理,但有两点局限: (1)数据库创建在内存卡中,大小受限,创建位置位于/data/data/应用程序名/databas ...

  7. android 读取sd卡中的图片

    一.获取读取SD卡的权限 <!--在SDCard中创建与删除文件权限  -->    <uses-permission android:name="android.perm ...

  8. 转-Android 之 使用File类在SD卡中读取数据文件

    如果需要在程序中使用sdcard进行数据的存储,那么需要在AndroidMainfset.xml文件中 进行权限的配置: Java代码:   <!-- 在sd中创建和删除文件的权限 --> ...

  9. 【Android 界面效果30】Android中ImageSwitcher结合Gallery展示SD卡中的资源图片

    本文主要是写关于ImageSwitcher结合Gallery组件如何展示SDCard中的资源图片,相信大家都看过API Demo 中也有关于这个例子的,但API Demo 中的例子是展示工程中Draw ...

随机推荐

  1. .net截取字符串

    string s=abcdeabcdeabcdestring[] sArray1=s.Split(new char[3]{c,d,e}) ;foreach(string i in sArray1)Co ...

  2. Delphi 正则表达式语法(5): 边界

    Delphi 正则表达式语法(5): 边界 // \b 单词边界 var   reg: TPerlRegEx; begin   reg := TPerlRegEx.Create(nil);   reg ...

  3. 了解IE中filter属性的应用!

    在设置不透明属性时,经常用opacity来增加层次感或者增加用户体验,但这个属性是css3属性,对于低级浏览器的兼容性来说就达不到预期的效果. 一般而言,我们都尽可能少用一些浏览私有属性-webkit ...

  4. linux(6/17)--文件打包上传和下载

    tar命令 命令功能 用来压缩和解压文件 命令格式 tar[必要参数][选择参数][文件] tar打包工具 -f ##指定生成包的名字,建议 -f单独写成一个参数 --delete filename ...

  5. React-native Android环境搭建

    基础安装 安装Homebrew Homebrew是Mac OSX的包管理器,我们需要通过Homebrew安装开发React Native的相关软件包. 如果不知道怎样安装Homebrew可以点这里:官 ...

  6. Django学习笔记之使用 Django项目开发框架

    Django 项目是一个定制框架,它源自一个在线新闻 Web 站点,于 2005 年以开源的形式被释放出来.Django 框架的核心组件有: 用于创建模型的对象关系映射 为最终用户设计的完美管理界面 ...

  7. iptables配置顺序-两条规则会忽略后边的

    oracle在centos本机能够正常访问,关闭防火墙也能够远程访问,但是一旦开启防火墙则不能远程访问 尝试添加规则iptables -A INPUT -m state --state NEW -m ...

  8. 20145217《网络对抗》 MSF基础应用

    20145217<网络对抗> MSF基础应用 MSF基础应用 1.实践任务 任务一:ms08_067渗透攻击 任务二:IE浏览器渗透攻击--MS12063安全漏洞 任务三:adobe渗透攻 ...

  9. QMap的使用

    1.定义 mapQMap<QString,QColor> map; 2.插入数据 map.insert("AA",RGB(255,0,0)); map.insert(& ...

  10. Spring框架下Junit测试

    Spring框架下Junit测试 一.设置 1.1 目录 设置源码目录和测试目录,这样在设置产生测试方法时,会统一放到一个目录,如果没有设置测试目录,则不会产生测试代码. 1.2 增加配置文件 Res ...