主类MUSICPLAYER类:
import java.util.*;
public class MusicPlayer implements ActionListener, Serializable,ControllerListener { private static final long serialVersionUID = 1L; private JFrame frame = null; private JPanel controlPanel = null; private JButton btnPlay = null; private JButton btnPre = null; private JButton btnNext = null; private JScrollPane listPane = null; private JList list = null; private DefaultListModel listModel = null; private JMenuBar menubar = null; private JMenu menuFile = null, menuAbout = null, menuMode = null; private JMenuItem itemOpen, itemOpens, itemExit, itemAbout; private JRadioButtonMenuItem itemSingle, itemSequence ,itemRandom; private ListItem currentItem = null; private static Player player = null; private boolean isPause = false; private int mode; private int currentIndex; private ImageIcon iconPlay = new ImageIcon(\ private ImageIcon iconPre = new ImageIcon(\ private ImageIcon iconNext = new ImageIcon(\ private ImageIcon iconPause = new ImageIcon(\ public static void main(String[] args) { new MusicPlayer(); } public MusicPlayer() { init(); } public void init() { frame = new JFrame(); frame.setTitle(\音乐播放器\ frame.setSize(400, 300); frame.setResizable(false); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); menubar = new JMenuBar();
menuFile = new JMenu(\文件\menuAbout = new JMenu(\关于\menuMode = new JMenu(\播放模式\itemOpen = new JMenuItem(\添加文件\itemOpens = new JMenuItem(\添加文件夹\itemExit = new JMenuItem(\退出\itemAbout = new JMenuItem(\关于\itemOpen.addActionListener(this); itemOpens.addActionListener(this); itemExit.addActionListener(this); itemAbout.addActionListener(this);
itemSequence = new JRadioButtonMenuItem(\顺序播放\itemSequence.setSelected(true);
itemSingle = new JRadioButtonMenuItem(\单曲循环\itemSequence.addActionListener(this);
itemRandom = new JRadioButtonMenuItem(\随机播放\itemRandom.addActionListener(this); itemSingle.addActionListener(this); ButtonGroup bg = new ButtonGroup(); bg.add(itemRandom); bg.add(itemSequence); bg.add(itemSingle);
menuFile.add(itemOpen); menuFile.add(itemOpens); menuFile.add(itemExit); menuAbout.add(itemAbout); menuMode.add(itemSequence); menuMode.add(itemSingle); menuMode.add(itemRandom); menubar.add(menuFile); menubar.add(menuAbout); menubar.add(menuMode); frame.setJMenuBar(menubar);
frame.setLayout(new BorderLayout()); controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout()); btnPlay = new JButton(iconPlay); btnPre = new JButton(iconPre); btnNext = new JButton(iconNext); btnPlay.addActionListener(this); btnPre.addActionListener(this); btnNext.addActionListener(this); controlPanel.add(btnPre); controlPanel.add(btnPlay); controlPanel.add(btnNext);
listPane = new JScrollPane(); listModel = load(); list = new JList(listModel); if (list.getSelectedIndex() == -1 && listModel.size() > 0) { currentItem = (ListItem) listModel.get(0); list.setSelectedIndex(0); currentIndex=0; } listPane.getViewport().add(list); list.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { if(player!=null) { player.close(); btnPlay.setIcon(iconPlay); } currentIndex = list.locationToIndex(e.getPoint()); currentItem = (ListItem) listModel.get(currentIndex); list.setSelectedIndex(currentIndex); play(); } } }); frame.setLayout(new BorderLayout()); frame.add(controlPanel, BorderLayout.NORTH); frame.add(listPane, BorderLayout.CENTER); frame.setVisible(true); }; public void actionPerformed(ActionEvent e) { if (e.getSource() == itemOpen) {// add files JFileChooser jfc = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter(\音乐文件\\ jfc.setFileFilter(filter); jfc.setFileSelectionMode(JFileChooser.FILES_ONLY); jfc.setMultiSelectionEnabled(true); if (jfc.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
File[] files = jfc.getSelectedFiles(); for (File f : files) { ListItem item = new ListItem(f.getName(), f.getAbsolutePath()); listModel.addElement(item); } } } else if (e.getSource() == itemOpens) {// add files in a directory JFileChooser jfc = new JFileChooser(); jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); if (jfc.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) { File directory = jfc.getSelectedFile(); { public boolean accept(File f) { if (f.getName().toLowerCase().endsWith(\f.getName().toLowerCase().endsWith(\ return true; return false; } }); for (File file : files) { ListItem item = new ListItem(file.getName(), file.getAbsolutePath()); listModel.addElement(item); } save(listModel); } } else if (e.getSource() == itemExit) { System.exit(0); } else if (e.getSource() == itemAbout) { JOptionPane.showMessageDialog(frame, \作者:陆鑫\ } else if (e.getSource() == btnPlay) { // play or pause play(); }
else if (e.getSource() == btnPre) { pre(); } else if (e.getSource() == btnNext) {// next music next(); } else if (e.getSource() == itemSequence) { mode = 0; } else if (e.getSource() == itemSingle) { mode = 1; } else if (e.getSource() == itemRandom) { mode = 2; } }
// play control /**
* 播放 */ public void play() { if (btnPlay.getIcon() == iconPlay) { if (isPause) { player.start(); isPause = false; } else { try { player = Manager.createPlayer(new MediaLocator(\currentItem.getPath())); player.addControllerListener(this); // 提取媒体内容 player.prefetch(); } catch (NoPlayerException e1) {