• android 歌词解析


    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.Hashtable;
    
    /**
     * <b>歌词解析类</b>
     * 
     * @author 席有芳
     * @QQ QQ:951868171
     * @version 1.0
     * @email xi_yf_001@126.com
     */
    public class LrcDecode {
    
        private Hashtable<String, String> lrcTable = null;
    
        /**
         * 解析Lrc
         */
        public LrcDecode readLrc(InputStream is) {
            lrcTable = new Hashtable<String, String>();
            try {
                BufferedReader bis = new BufferedReader(new InputStreamReader(is, "gbk"));
                String str = null;
                while ((str = bis.readLine()) != null) {
                    decodeLine(str);
                }
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
                lrcTable = null;
            }
            return this;
        }
    
        /**
         * 单行解析
         */
        private LrcDecode decodeLine(String str) {
    
            if (str.startsWith("[ti:")) {// 歌曲名
                lrcTable.put("ti", str.substring(4, str.lastIndexOf("]")));
    
            } else if (str.startsWith("[ar:")) {// 艺术家
                lrcTable.put("ar", str.substring(4, str.lastIndexOf("]")));
    
            } else if (str.startsWith("[al:")) {// 专辑
                lrcTable.put("al", str.substring(4, str.lastIndexOf("]")));
    
            } else if (str.startsWith("[by:")) {// 作词
                lrcTable.put("by", str.substring(4, str.lastIndexOf("]")));
    
            } else if (str.startsWith("[la:")) {// 语言
                lrcTable.put("la", str.substring(4, str.lastIndexOf("]")));
    
            } else {
                // 歌词正文
                int startIndex = -1;
                while ((startIndex = str.indexOf("[", startIndex + 1)) != -1) {
                    int endIndex = str.indexOf("]", startIndex + 1);
                    // 添加时间戳格式
                    lrcTable.put(strToLongToTime(str.substring(startIndex + 1, endIndex)) + "",
                            str.substring(str.lastIndexOf("]") + 1, str.length()));
                }
            }
            return this;
        }
    
        /**
         * 获取解析成功的歌词
         */
        public Hashtable<String, String> getLrcTable() {
            return lrcTable;
        }
    
        /**
         * 保证时间格式一致 为m:ss
         * 
         * @param str
         *            时间字符
         * @return 判断用的时间符
         */
        private String strToLongToTime(String str) {
            // System.out.println(str);
            int m = Integer.parseInt(str.substring(0, str.indexOf(":")));
            int s = 0;
            int ms = 0;
    
            // 判断歌词时间是否有毫秒
            if (str.indexOf(".") != -1) {
                s = Integer.parseInt(str.substring(str.indexOf(":") + 1, str.indexOf(".")));
                ms = Integer.parseInt(str.substring(str.indexOf(".") + 1, str.length()));
            } else {
                s = Integer.parseInt(str.substring(str.indexOf(":") + 1, str.length()));
            }
            // System.out.println(timeMode(m * 60000 + s * 1000 + ms * 10));
            return timeMode(m * 60000 + s * 1000 + ms * 10);
        }
    
        /**
         * 返回时间
         * 
         * @param time
         *            毫秒时间
         */
        public static String timeMode(int time) {
            int tmp = (time / 1000) % 60;
            if (tmp < 10)
                return time / 60000 + ":" + "0" + tmp;
            else
                return time / 60000 + ":" + tmp;
        }
    
        public static void main(String[] str) {
            try {
                FileInputStream f = new FileInputStream("D:" + File.separator + "google" + File.separator + "一点点.lrc");
                LrcDecode lrcDecode = new LrcDecode();
                lrcDecode.readLrc(f);
                if (lrcDecode.getLrcTable() != null) {
                    // 转换时间并输出内容,3000为当前时间currentTime
                    System.out.println(lrcDecode.getLrcTable().get(LrcDecode.timeMode(3000)));
                }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    }
  • 相关阅读:
    JSONModel的使用
    gitHub那些优秀的库和想要实现的效果
    CALayer的mask属性
    透明的UITableView
    iOS 常用随机数
    UIView的 形变属性transform
    ARGB色彩模式
    封闭折线图形的渐变色
    iOS系统日历选择问题
    每天积累一点新知识
  • 原文地址:https://www.cnblogs.com/yzjT-mac/p/5831150.html
Copyright © 2020-2023  润新知