原文来源:http://hi.baidu.com/282919088/item/a45efd2d465d6e0b42634ac5
package 娱乐.播放器; import java.awt.Container; import java.awt.GridLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.File; import java.io.FileReader; import java.util.Vector; import javax.media.bean.playerbean.MediaPlayer; import javax.swing.ImageIcon; import javax.swing.JComboBox; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class MP3 extends JFrame implements Runnable { Vector cunci = new Vector(); String string = ""; public MediaPlayer ad = new MediaPlayer(); boolean fo = true, foo = false, shen = false; int fenzhong, miaozhong, zongshijian = 0;; int a = 0, b = 0, you = 0; JLabel jt = new JLabel(); ImageIcon bofang = new ImageIcon("pic/a_bf.gif"); ImageIcon tingzhi = new ImageIcon("pic/a_tz.gif"); ImageIcon xiayi = new ImageIcon("pic/a_dyd.gif"); ImageIcon shangyi = new ImageIcon("pic/a_zhyd.gif"); ImageIcon caidan = new ImageIcon("pic/a_list.gif"); JLabel bf = new JLabel(); JLabel tz = new JLabel(); JLabel xy = new JLabel(); JLabel sy = new JLabel(); JLabel cd = new JLabel(); Vector musicc = new Vector(); JComboBox dizhi; JLabel shijian = new JLabel(); private int zong = 0; JTextField jlb = new JTextField(); public MP3() { super("木人播放器(同步歌词mp3格式)"); Container c = getContentPane(); c.setLayout(new GridLayout(2, 1)); JPanel jpp = new JPanel(); musicc.add("---------------添加音乐----------------"); dizhi = new JComboBox(musicc); dizhi.setMaximumRowCount(5); // 最大显示行数 bf.setIcon(bofang); tz.setIcon(tingzhi); xy.setIcon(xiayi); sy.setIcon(shangyi); cd.setIcon(caidan); jpp.add(xy); jpp.add(bf); jpp.add(sy); jpp.add(cd); shijian.setText(" 0:00"); jpp.add(shijian); jpp.add(dizhi); c.add(jpp); jlb.setEditable(false); jlb.setHorizontalAlignment(JTextField.CENTER); c.add(jlb); setSize(450, 100); setVisible(true); shijian.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent event) { if (event.getClickCount() == 2) if (bf.getIcon().toString().equals("pic/a_tz.gif")) { if (foo == true) { foo = false; shen = true; } else { foo = true; shen = false; } } } }); bf.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent event) { File f = new File(dizhi.getSelectedItem().toString()); if (f.exists()&&dizhi.getSelectedItem().toString().matches("[\\S\\s]*.mp3")) { if (bf.getIcon().toString().equals("pic/a_bf.gif")) { try { ad.stop(); } catch (Exception e) { } bf.setIcon(tingzhi); ms(dizhi.getSelectedItem().toString()); cunci.clear(); cunci = shilihua(daoxu(dizhi.getSelectedItem().toString()) + "lrc"); if (cunci.size() == 0){ jlb.setText("无歌词··木人制作..Mr.cn"); } ad.start();; fen(); foo = true; } else { bf.setIcon(bofang); ad.stop(); a = 0; b = 0; you = 0; jlb.setText(""); foo = false; shen = false; shijian.setText(" 0:00"); } }else{ JOptionPane.showMessageDialog(null, "音乐文件格式不正常或者已经不存在"); } } }); xy.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent event) { int i = dizhi.getSelectedIndex() - 1; if (i < dizhi.getItemCount() && i >= 0) { dizhi.setSelectedIndex(i); File f = new File(dizhi.getSelectedItem().toString()); if (f.exists()&&dizhi.getSelectedItem().toString().matches("[\\S\\s]*.mp3")) { if (ad != null) { bf.setIcon(bofang); ad.stop(); a = 0; b = 0; you = 0; jlb.setText(""); foo = false; shen = false; } cunci.clear(); cunci = shilihua(daoxu(dizhi.getSelectedItem() .toString()) + "lrc"); if (cunci.size() == 0){ jlb.setText("无歌词··木人制作..Mr.cn"); } ms(dizhi.getItemAt(i).toString()); ad.start(); fen(); bf.setIcon(tingzhi); foo = true; }else{ JOptionPane.showMessageDialog(null, "音乐文件格式不正常或者已经不存在"); } } else { JOptionPane.showMessageDialog(null, "已经到了顶部"); } } }); sy.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent event) { int i = dizhi.getSelectedIndex() + 1; if (i < dizhi.getItemCount() && i >= 0) { dizhi.setSelectedIndex(i); File f = new File(dizhi.getSelectedItem().toString()); if (f.exists()&&dizhi.getSelectedItem().toString().matches("[\\S\\s]*.mp3")) { if (ad != null) { bf.setIcon(bofang); ad.stop(); a = 0; b = 0; you = 0; jlb.setText(""); foo = false; shen = false; } cunci.clear(); cunci = shilihua(daoxu(dizhi.getSelectedItem() .toString()) + "lrc"); if (cunci.size() == 0){ jlb.setText("无歌词··木人制作..Mr.cn"); } ms(dizhi.getItemAt(i).toString()); ad.start(); fen(); bf.setIcon(tingzhi); foo = true; }else{ JOptionPane.showMessageDialog(null, "音乐文件格式不正常或者已经不存在"); } } else { JOptionPane.showMessageDialog(null, "已经到了尾部"); } } }); cd.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent event) { JFileChooser fileChooser = new JFileChooser(); // 实例化文件选择器 fileChooser .setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // 设置文件选择模式,此处为文件和目录均可 fileChooser.setCurrentDirectory(new File(".")); // 设置文件选择器当前目录 fileChooser .setFileFilter(new javax.swing.filechooser.FileFilter() { public boolean accept(File file) { // 可接受的文件类型 String name = file.getName().toLowerCase(); return name.endsWith(".mp3") || file.isDirectory(); } public String getDescription() { // 文件描述 return "音乐文件(*.mp3)"; } }); if (fileChooser.showOpenDialog(MP3.this) == JFileChooser.APPROVE_OPTION) { // 弹出文件选择器,并判断是否点击了打开按钮 String fileName = fileChooser.getSelectedFile() .getAbsolutePath(); // 得到选择文件或目录的绝对路径 musicc.add(fileName); } } }); } public void fen() { do { try { Thread.sleep(500); } catch (InterruptedException ew) { } zong = (int) ad.getDuration().getSeconds(); } while (zong > 10000); int aaa = zong; fenzhong = (int) aaa / 60; miaozhong = (int) aaa % 60; String ss = fenzhong + ":" + miaozhong; } public Vector shilihua(String url) { Vector vv = new Vector(); try { FileReader in = new FileReader(url); int a = 0; while ((a = in.read()) != -1) { if ((char) a != '\n') { string += (char) a; } else if ((char) a == '\n') { vv.add(string); string = ""; } } } catch (Exception e) { System.out.println("错误"); } return vv; } public void ms(String url) { try { ad.setMediaLocation("file:/" + url); } catch (Exception e) { } } public static void main(String[] args) { MP3 s = new MP3(); Thread t1 = new Thread(s); t1.start(); s.setDefaultCloseOperation(3); } public String daoxu(String fileName) { String mc = ""; int a = 0; for (int i = fileName.length(); i >= 1; i--) { if (fileName.charAt(i - 1) == '.') { break; } else { a++; } } mc = fileName.substring(0, fileName.length() - a); return mc; } public StringBuffer geci(String fileName) { String mc = ""; for (int i = fileName.length(); i >= 1; i--) { if (fileName.charAt(i - 1) == ']') break; mc += fileName.charAt(i - 1); } StringBuffer buffer = new StringBuffer(mc); StringBuffer mm = buffer.reverse(); return mm; } public void run() { while (true) { if (foo) { a++; you++; if (a == miaozhong && b == fenzhong) { bf.setIcon(bofang); a = 0; b = 0; you = 0; shijian.setText(" 0:00"); ad.stop(); foo = false; } if (a >= 60) { b++; a = 0; } if ((a + "").length() == 1) { for (int i = 0; i < cunci.size(); i++) { if (cunci.get(i).toString() .indexOf("[0" + b + ":0" + a) >= 0) { jlb.setText(geci(cunci.get(i).toString()).toString()); } } shijian.setText(" " + b + ":0" + a); } else { for (int i = 0; i < cunci.size(); i++) { if (cunci.get(i).toString().indexOf("[0" + b + ":" + a) >= 0) { jlb.setText(geci(cunci.get(i).toString()).toString()); } } shijian.setText(" " + b + ":" + a); } } if (shen) { a++; you++; if (a == miaozhong && b == fenzhong) { bf.setIcon(bofang); a = 0; b = 0; you = 0; shijian.setText(" 0:00"); ad.stop(); shen = false; } if (a >= 60) { b++; a = 0; } if (miaozhong - a < 0) { fenzhong--; miaozhong += 60; } if (miaozhong - a > 59) { miaozhong -= 60; fenzhong++; } if (((miaozhong - a) + "").length() == 1) { for (int i = 0; i < cunci.size(); i++) { if (cunci.get(i).toString().indexOf("[0" + b + ":0" + a) >= 0) { jlb.setText(geci(cunci.get(i).toString()).toString()); } } shijian.setText("-" + (fenzhong - b) + ":0" + (miaozhong - a)); } else { for (int i = 0; i < cunci.size(); i++) { if (cunci.get(i).toString().indexOf("[0" + b + ":" + a) >= 0) { jlb.setText(geci(cunci.get(i).toString()).toString()); } } shijian.setText("-" + (fenzhong - b) + ":" + (miaozhong - a)); } } try { { Thread.sleep(1000); } } catch (InterruptedException ie) { } } } }
需要的图片。。 放在 同目录下 pic 文件夹里面
没有jfm包的可以去 http://mr87.ys168.com/ 我的E盘 java工具 下载。(2个都要安装,一个jar。一个EXE运行程序)