这是我们JAVA课程的作业,笔者还是花了不少时间来完成的,虽然还是有点烂。。。
直接贴代码~
package MyMusicPlayer; import java.awt.*; import java.awt.event.*; import java.io.File; import java.util.ArrayList; import java.util.List; import javax.media.*; import javax.swing.*; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import javax.swing.filechooser.FileFilter; public class MPlayer implements ActionListener, ControllerListener, Runnable { // 界面 private JFrame frame = new JFrame("音乐播放器"); private JButton open = new JButton("打开"); private JButton stop = new JButton("停止"); private JButton music = new JButton("关于"); private JButton next = new JButton("下一首"); private JButton last = new JButton("上一首"); private JPanel progress = new JPanel(); private JPanel progress2 = new JPanel(); private JPanel buttonPanel = new JPanel(); private JPanel buttonPanel2 = new JPanel(); private JPanel buttonPanel3 = new JPanel(); private JButton timeInformation = new JButton(); private JSlider timeSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 100, 0); // 播放 int index; int count; int countSecond; int newtime = 0; int ischanging = 0; int ispressing = 0; private Player player = null; private MP3FileChooser fileChooser = new MP3FileChooser(); File currentFile = null; List<File> currentFiles = null; private File currentDirectory = null; /** * ActionListener override */ public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if (cmd.equals("打开")) { if (player == null) { if (fileChooser.showOpenDialog(frame) == MP3FileChooser.APPROVE_OPTION) { this.currentFile = fileChooser.getSelectedFile(); File cd = fileChooser.getCurrentDirectory(); if (cd != this.currentDirectory || this.currentDirectory == null) { FileFilter[] fileFilters = fileChooser .getChoosableFileFilters(); File files[] = cd.listFiles(); this.currentFiles = new ArrayList<File>(); for (File file : files) { for (FileFilter filter : fileFilters) { if (!file.isDirectory() && filter.accept(file)) { this.currentFiles.add(file); } } } } index = currentFiles.indexOf(currentFile); count = currentFiles.size(); PlayFile(); } } else { player.start(); } } else if (cmd.equals("停止")) { if (player != null) { player.close(); player = null; open.setText("打开"); next.setText("下一首"); last.setText("上一首"); music.setText("关于"); timeInformation.setText("当前时间:00:00 / 总时间:00:00"); } } else if (cmd.equals("暂停")) { open.setText("播放"); player.stop(); } else if (cmd.equals("播放")) { open.setText("暂停"); player.start(); } else if (cmd.equals("下一首") || cmd.startsWith("下一首")) { if (player != null) { nextMP3(); } } else if (cmd.equals("上一首") || cmd.startsWith("上一首")) { if (player != null) { lastMP3(); } } else { JOptionPane.showMessageDialog(null, "华南理工大学 软件学院 周顺风"); } } /** * ControllerListener override */ public void controllerUpdate(ControllerEvent e) { if (e instanceof EndOfMediaEvent) { nextMP3(); } } /** * 播放MP3文件主函数 */ public void PlayFile() { try { player = Manager.createRealizedPlayer(currentFiles.get(index).toURI().toURL()); player.prefetch(); player.setMediaTime(new Time(0.0));// 从莫个时间段开始播放 player.addControllerListener(this); player.start(); } catch (Exception e1) { dealError(); return; } this.setFrame(); } /** * 获取MP3歌曲名 * * @MP3文件路径 * @歌曲名 */ public String getMP3Name(String str) { int i; for (i = str.length() - 1; i > 0; i--) { if (str.charAt(i) == '\\') break; } str = str.substring(i + 1, str.length() - 4); return str; } /** * 下一首 实现函数 */ public void nextMP3() { if (this.currentFiles != null && !this.currentFiles.isEmpty()) { if ( ++index == this.currentFiles.size()) { index = 0; } player.stop(); PlayFile(); } } /** * 上一首实现函数 */ public void lastMP3() { if (this.currentFiles != null && !this.currentFiles.isEmpty()) { if ( index-- == 0) { index = this.currentFiles.size() - 1; } player.stop(); PlayFile(); } } /** * 播放界面设置 */ public void setFrame() { open.setText("暂停"); next.setText("下一首 " + getMP3Name(this.currentFiles.get( index == count - 1 ? 0 : index + 1).toString())); last.setText("上一首 " + getMP3Name(this.currentFiles.get( index == 0 ? count - 1 : index - 1).toString())); music.setText("正在播放:"+getMP3Name(this.currentFiles.get(index).toString())); countSecond = (int)player.getDuration().getSeconds(); timeSlider.setMaximum(countSecond); timeSlider.setValue(0); newtime = 0; }; /** * 异常处理 * 在播放队列中直接删除错误文件,下一首播放 */ public void dealError() { currentFiles.remove(index); if( --count == index ) index = 0; if( count == 0) player = null; else PlayFile(); } /** * MP3文件筛选内部类 * * @author BlackStar * */ @SuppressWarnings("serial") class MP3FileChooser extends JFileChooser { public MP3FileChooser() { super(); setAcceptAllFileFilterUsed(false); addFilter(); } public MP3FileChooser(String currentDirectoryPath) { super(currentDirectoryPath); setAcceptAllFileFilterUsed(false); addFilter(); } private void addFilter() { this.addChoosableFileFilter(new MyFileFilter( new String[] { ".MP3" }, "MP3音乐文件")); } } /** * MP3文件筛选辅助内部类 * * @author BlackStar * */ class MyFileFilter extends FileFilter { String[] suffarr; String decription; public MyFileFilter() { super(); } public MyFileFilter(String[] suffarr, String decription) { super(); this.suffarr = suffarr; this.decription = decription; } public boolean accept(File f) { for (String s : suffarr) { if (f.getName().toUpperCase().endsWith(s)) { return true; } } return f.isDirectory(); } public String getDescription() { return this.decription; } } /** * MPlayer构造器 */ public MPlayer() { open.addActionListener(this); stop.addActionListener(this); next.addActionListener(this); last.addActionListener(this); music.addActionListener(this); timeInformation.addActionListener(this); frame.setLayout(new FlowLayout()); buttonPanel.add(open); buttonPanel.add(stop); frame.add(buttonPanel); timeSlider.setMajorTickSpacing(1); timeSlider.setPaintTicks(true); timeSlider.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent arg0) { if (player != null && ispressing == 1) { newtime = (int)((JSlider)arg0.getSource()).getValue(); timeInformation.setText("当前时间:"+newtime/60+":"+newtime%60+" 总时间: "+countSecond/60+":"+countSecond%60); ischanging = 1; } } }); timeSlider.addMouseListener(new MouseAdapter(){ public void mousePressed(MouseEvent arg0) { ispressing = 1; } public void mouseReleased(MouseEvent arg0) { ispressing = 0; } }); timeInformation.setText("当前时间:00:00 / 总时间:00:00"); progress.add(timeInformation,BorderLayout.NORTH); progress2.add(timeSlider,BorderLayout.SOUTH); frame.add(progress); frame.add(music); frame.add(progress2); buttonPanel2.add(last); buttonPanel3.add(next); frame.add(buttonPanel2); frame.add(buttonPanel3); frame.setSize(220, 280); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = frame.getSize(); if (frameSize.height > screenSize.height) { frameSize.height = screenSize.height; } if (frameSize.width > screenSize.width) { frameSize.width = screenSize.width; } frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); frame.setVisible(true); frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } @Override /** * 读取显示时间进度条 */ public void run() { while(true) { sleep(); if(player != null) { if(ispressing == 0) { if(ischanging == 1) { newtime = timeSlider.getValue(); player.setMediaTime(new Time(((long)newtime)*1000000000)); ischanging = 0; } else { newtime = (int)player.getMediaTime().getSeconds(); timeSlider.setValue(newtime); timeInformation.setText("当前时间:"+newtime/60+":"+newtime%60+" 总时间: "+countSecond/60+":"+countSecond%60); } } } } } /** * while暂停 */ public void sleep() { try { Thread.sleep(10); } catch (InterruptedException e) { } } /** * 驱动器 */ public static void main(String[] args) { MPlayer play = new MPlayer(); Thread timeRun = new Thread(play); timeRun.start(); } }