Android自定义歌词同步控件
时间:2025-03-10
时间:2025-03-10
该文档讲述在android中如何自定义歌词控件同步显示歌词,文档讲述歌词控件用法,程序中所需要的也只有2个类,一个歌词对象,一个歌词控件,兼容性很强。无须配置特别的xml文件,简单易用
Android自定义歌词同步控件
Android音乐播放器中,播放音乐的类(即MediaPlayer)在播放音乐的时候,通过MediaPlayer的getCurrentPosition方法可以得到当前音乐播放的流进度,通过getDuration可以得到当前音乐总的流大小。因此,我们可以通过这两个方法来判断同步的音乐歌词播放进度。下面,该文档将为大家实现歌词同步,其他音乐播放的东西一概不涉及。
歌词同步相关两个类: SongLyric(歌词对象) 歌词对象实例化即可使用,但必须保证该歌词对象验证通过,即歌词文件存在,切正确实例化。
LyricView(Android自定义歌词控件) 该歌词控件不可以写在xml配置文件中,必须使用一个layout布局控件存放,使用的时候先从Activity中得到layout,然后再将该歌词控件通过layout的getContext的参数实例化,最后添加到layout中,并且将对应的歌词对象SongLyric设置到歌词控件中。最后,歌词控件要做到与音乐同步的效果,还得时时刷新歌词控件,这样就有了歌词同步以及滚动的效果。具体实现方法下面讲到,先看看这两个类源码:
SongLyric类,该对象和网上大部分歌词对象一样,这里为了和歌词控件LyricView配套,
该文档讲述在android中如何自定义歌词控件同步显示歌词,文档讲述歌词控件用法,程序中所需要的也只有2个类,一个歌词对象,一个歌词控件,兼容性很强。无须配置特别的xml文件,简单易用
// 歌手名 private String artist = ""; // 专辑名 private String album = ""; // 偏移时间 private long offset = 0; // 最大时间 private long maxTime = 0; // 歌词内容 private Map<Long, String> lrcs = new HashMap<Long, String>(); // 验证是否通过 private boolean valid = false; public SongLyric(String url) { File file = new File(url); if (file.exists()) { try { // 构建读取器 BufferedReader br = new BufferedReader(new InputStreamReader( new FileInputStream(file), "gbk")); String line = null; while ((line = br.readLine()) != null) { dealLine(line); } valid = true; } catch (Exception e) { System.out.println("Exception"); } } } public String getTitle() { return this.title; } public String getArtist() { return this.artist; } public String getAlbum() { return this.album; } public boolean isValid() {
该文档讲述在android中如何自定义歌词控件同步显示歌词,文档讲述歌词控件用法,程序中所需要的也只有2个类,一个歌词对象,一个歌词控件,兼容性很强。无须配置特别的xml文件,简单易用
return this.valid; } public long getMaxTime() { return this.maxTime; } public void setMaxTime(long time) { this.maxTime = time; } /** * 获取该时间应当显示的歌词 * * @param ls * @return */ public String get(long ls) { long time = ls + offset; Long curr = -1l; for (Long l : lrcs.keySet()) { curr = l > time ? curr : l < curr ? curr : l; } return lrcs.get(curr); } /** * 获取该时间所要显示的歌词初始时
间的索引 * * @param ls * @return */ public int getIndex(long ls) { Long[] ts = getAllTimes(); for (int i = 0; i < ts.length - 1; i++) { if (ls + offset >= ts[i] && ls + offset < ts[i + 1]) { return i; } } return ts.length - 1; } /** * 获取该时间与歌词初始时间差值
该文档讲述在android中如何自定义歌词控件同步显示歌词,文档讲述歌词控件用法,程序中所需要的也只有2个类,一个歌词对象,一个歌词控件,兼容性很强。无须配置特别的xml文件,简单易用
* * @param ls * @return */ public int getOffset(long ls) { Long[] ts = getAllTimes(); int index = getIndex(ls); if (index < ts.length && index >= 0) { return (int) (ls + offset - ts[index]); } return 0; } /** * 获取该时间段播放的歌词共播放时间 * * @param ls * @return */ public int getNextTime(long ls) { Long[] ts = getAllTimes(); int index = getIndex(ls); if (index < ts.length - 1) { return (int) (ts[index + 1] - ts[index]); } return 0; } /** * 处理歌词行 * * @param line */ private void dealLine(String line) { if (line != null && !line.equals("")) { if (line.startsWith("[ti:")) {// 标题 title = line.substring(4, line.length() - 1); } else if (line.startsWith("[ar:")) {// 歌手 artist = line.substring(4, line.length() - 1); } else if (line.startsWith("[al:")) {// 专辑 album = line.substring(4, line.length() - 1); } else if (line.startsWith("[offset:")) {// 专辑 offset = Long.parseLong(line.substring(8, line.length() - 1)); } else {
该文档讲述在android中如何自定义歌词控件同步显示歌词,文档讲述歌词控件用法,程序中所需要的也只有2个类,一个歌词对象,一个歌词控件,兼容性很强。无须配置特别的xml文件,简单易用
// 该行歌词内容 Pattern ptn = http://pile("\\[(\\d{2}:\\d{2}\\.\\d{2})\\]"); Matcher mth = ptn.matcher(line); while (mth.find()) { // 得到时间点 long time = strToLong(mth.group(1)); // 得到时间点后的内容 String[] str = ptn.split(line); String lrc = str.length > 0 ? str[str.length - 1] : ""; lrcs.put(time, lrc); maxTime = maxTime > time ? maxTime : time; } } } } /** * 将 00:00.00 格式的歌词时间转换为 long * * @param timeStr * @return */ public static long strToLong(String timeStr) { String[] s = timeStr.split(":"); int min = Integer.parseInt(s[0]); String[] ss = s[1].split("\\."); …… 此处隐藏:6316字,全部文档内容请下载后查看。喜欢就下载吧 ……
上一篇:三年级下册语文期未试卷
下一篇:高考英语短文改错考点总汇分类解析