纯java编写的MP3播放器,用了JMF扩展包,只能播放MP3,而且播放的频率好像还有限(有些播放不了)。
首先定义一些全局变量(个人习惯。。):
package MP3.main;
import java.awt.Component;
import java.util.HashMap;
import javax.media.Player;
import javax.swing.DefaultListModel;
import javax.swing.JList;
public class PLAYER {
public static Player player;//播放器
public static Component control;//控制器
public static DefaultListModel<String> SongModel=new DefaultListModel<String>();//菜单列表模型
public static JList<String> SongList=new JList<String>(PLAYER.SongModel);//菜单列表
public static HashMap<String,String> SongMap=new HashMap<String,String>();//歌曲名和存储位置映射表
public static String Playing;//正在播放的歌曲名
public static enum PlayState{
//播放模式
SINGLE_PLAY,//单曲播放
SINGLE_CYCLE,//单曲循环
ORDER_PLAY,//顺序播放
RANDOM_PLAY,//随机播放
ALL_CYCLE;//全部循环
}
public static PlayState State=PLAYER.PlayState.SINGLE_PLAY;//当前的歌曲播放模式
}
接下来是按钮面板:
//设置按钮面板
public class ButtonPanel extends JPanel {
private static final long serialVersionUID = 1L;
public ButtonPanel(){
setVisible(true);
setBackground(null);
setLayout(null);
JButton PlayButton=new JButton(new ImageIcon("src//11.png"));
JButton PauseButton=new JButton(new ImageIcon("src//21.png"));
JButton StopButton=new JButton(new ImageIcon("src//31.png"));
ButtonAction PlayAction=new ButtonAction("播放");
ButtonAction PauseAction=new ButtonAction("暂停");
ButtonAction StopAction=new ButtonAction("停止");
PlayButton.addActionListener(PlayAction);
PauseButton.addActionListener(PauseAction);
StopButton.addActionListener(StopAction);
add(PlayButton);
add(PauseButton);
add(StopButton);
PlayButton.setBounds(1, 1, 30, 28);
PauseButton.setBounds(52, 1, 30, 28);
StopButton.setBounds(104, 1, 30, 28);
PlayButton.setBorder(null);
PauseButton.setBorder(null);
StopButton.setBorder(null);
PlayButton.setContentAreaFilled(false);
PlayButton.setRolloverIcon(new ImageIcon("src//12.png"));
PlayButton.setPressedIcon(new ImageIcon("src//13.png"));
PauseButton.setContentAreaFilled(false);
PauseButton.setRolloverIcon(new ImageIcon("src//22.png"));
PauseButton.setPressedIcon(new ImageIcon("src//23.png"));
StopButton.setContentAreaFilled(false);
StopButton.setRolloverIcon(new ImageIcon("src//32.png"));
StopButton.setPressedIcon(new ImageIcon("src//33.png"));
}
}
class ButtonAction implements ActionListener{
public ButtonAction(String act){
act1 = act;
}
public void actionPerformed(ActionEvent event){
if (act1.equals("播放")){
if (PLAYER.player != null) {
PLAYER.player.start();
}
return;
}
if (act1.equals("暂停")){
if (PLAYER.player != null) {
PLAYER.player.stop();
}
return;
}
if (act1.equals("停止")){
if (PLAYER.player != null) {
PLAYER.player.stop();
PLAYER.player.setMediaTime(new Time(0));
}
return;
}
}
private String act1;
}
菜单面板:
//歌曲单列表
public class JListPanel extends JPanel{
private static final long serialVersionUID = 1L;
public JListPanel(){
PLAYER.SongList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//设置文件选择为单文件
delete.addActionListener(new ActionLinstener());
change.addActionListener(new ActionLinstener());
PLAYER.SongList.setBackground(Color.DARK_GRAY);
PLAYER.SongList.setForeground(new Color(255,113,72));
PLAYER.SongList.setSelectionBackground(Color.DARK_GRAY);
PLAYER.SongList.setSelectionForeground(new Color(121,220,241));
jpm.add(delete);//加入右键菜单项目
jpm.add(change);//加入右键菜单项目
jsp.add(jpm);//为列表加入右键菜单
this.add(jsp);
jsp.setPreferredSize(new Dimension(243,340));
jsp.setBorder(null);
this.setVisible(true);
PLAYER.SongList.addMouseListener(new ListMouseListener());
}
class ListMouseListener implements MouseListener{
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
PLAYER.SongList.setSelectedIndex(PLAYER.SongList.locationToIndex(e.getPoint()));
}
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()==2&&e.getButton()==1){
JList source=(JList)e.getSource();
if(PLAYER.player!=null&&PLAYER.control!=null){
PLAYER.player.stop();
MP3_Player.pf.remove(PLAYER.control);
}
try {
PLAYER.player=Manager.createPlayer(new MediaLocator("file:"+PLAYER.SongMap.get(source.getSelectedValue())));
PLAYER.Playing=source.getSelectedValue().toString();
PLAYER.player.prefetch();
PLAYER.player.start();
PLAYER.player.addControllerListener(new ControlListener(MP3_Player.pf));
} catch (Exception e2) {
System.out.println(e2.getStackTrace());
}
}
if(e.getButton()==3&&PLAYER.SongList.getSelectedIndex()>=0){
PLAYER.SongList.setSelectedIndex(PLAYER.SongList.getSelectedIndex());
jpm.show(PLAYER.SongList, e.getX(), e.getY());
}
}
}
class ActionLinstener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("删除")){
//如果删除的项目是正在播放的歌曲
if(PLAYER.Playing.equals(PLAYER.SongList.getSelectedValue())){
PLAYER.player.stop();
MP3_Player.pf.remove(PLAYER.control);
int ex=PLAYER.SongModel.indexOf(PLAYER.SongList.getSelectedValue());
PLAYER.SongModel.removeElement(PLAYER.SongList.getSelectedValue());
PLAYER.SongMap.remove(PLAYER.SongList.getSelectedValue());
//删除的曲目不是最后一曲
if(ex<PLAYER.SongModel.size()){
try {
PLAYER.player=Manager.createPlayer(new MediaLocator("file:"+PLAYER.SongMap.get(PLAYER.SongModel.get(ex))));
PLAYER.player.prefetch();
PLAYER.player.start();
PLAYER.Playing=PLAYER.SongModel.get(ex);
PLAYER.player.addControllerListener(new ControlListener(MP3_Player.pf));
} catch (Exception e2) {
System.out.println(e2.getStackTrace());
}
}
//删除的曲目是最后一曲
else if(ex==PLAYER.SongModel.size()){
try {
System.out.println(PLAYER.SongMap.get(PLAYER.SongModel.get(0)));
PLAYER.player=Manager.createPlayer(new MediaLocator("file:"+PLAYER.SongMap.get(PLAYER.SongModel.get(0))));
PLAYER.player.prefetch();
PLAYER.player.start();
PLAYER.Playing=PLAYER.SongModel.get(0);
PLAYER.player.addControllerListener(new ControlListener(MP3_Player.pf));
} catch (Exception e2) {
System.out.println(e2.getStackTrace());
}
}
else if(PLAYER.SongModel.isEmpty()==true){
PLAYER.player.close();
}
}
else{
PLAYER.SongModel.removeElement(PLAYER.SongList.getSelectedValue());
PLAYER.SongMap.remove(PLAYER.SongList.getSelectedValue());
}
}
if(e.getActionCommand().equals("修改文件名")){
ChangeNamePanel cn=new ChangeNamePanel(PLAYER.SongList.getSelectedValue());
cn.setVisible(true);
cn.setAlwaysOnTop(true);
}
}
}
JScrollPane jsp=new JScrollPane(PLAYER.SongList);//加入滚动条
JMenuItem delete=new JMenuItem("删除");
JMenuItem change=new JMenuItem("修改文件名");
JPopupMenu jpm=new JPopupMenu();//列表右键菜单
}
播放模式选择面板:
//播放模式选择面板
public class RadioButtonPanel extends JPanel{
private static final long serialVersionUID = 1L;
public RadioButtonPanel(){
setVisible(true);
setBackground(null);
add(SinglePlayButton);
add(SingleCycleButton);
add(OrderPlayButton);
add(RandomPlayButton);
add(AllCycleButton);
ButtonSet(SinglePlayButton);
ButtonSet(SingleCycleButton);
ButtonSet(OrderPlayButton);
ButtonSet(RandomPlayButton);
ButtonSet(AllCycleButton);
bg.add(SinglePlayButton);
bg.add(SingleCycleButton);
bg.add(OrderPlayButton);
bg.add(RandomPlayButton);
bg.add(AllCycleButton);
}
private void ButtonSet(JRadioButton button){
button.setBackground(null);
button.setForeground(new Color(215,212,212));
button.setBorder(new EmptyBorder(0, 0, 0, 0));
button.addActionListener(new JRadioButtonAction(button.getActionCommand()));
}
ButtonGroup bg=new ButtonGroup();
JRadioButton SinglePlayButton=new JRadioButton("单曲播放",true);
JRadioButton SingleCycleButton=new JRadioButton("单曲循环",false);
JRadioButton OrderPlayButton=new JRadioButton("顺序播放",false);
JRadioButton RandomPlayButton=new JRadioButton("随机播放",false);
JRadioButton AllCycleButton=new JRadioButton("全部循环",false);
}
class JRadioButtonAction implements ActionListener{
public JRadioButtonAction(String act){
this.act=act;
}
public void actionPerformed(ActionEvent e) {
if(act.equals("单曲播放")){
System.out.println("单曲播放");
PLAYER.State=PLAYER.PlayState.SINGLE_PLAY;
}
else if(act.equals("单曲循环")){
System.out.println("单曲循环");
PLAYER.State=PLAYER.PlayState.SINGLE_CYCLE;
}
else if(act.equals("顺序播放")){
System.out.println("顺序播放");
PLAYER.State=PLAYER.PlayState.ORDER_PLAY;
}
else if(act.equals("随机播放")){
System.out.println("随机播放");
PLAYER.State=PLAYER.PlayState.RANDOM_PLAY;
}
else if(act.equals("全部循环")){
System.out.println("全部循环");
PLAYER.State=PLAYER.PlayState.ALL_CYCLE;
}
}
private String act;
}
查找文件面板:
//查找文件面板
public class SearchPanel extends JPanel implements ActionListener{
private static final long serialVersionUID = 1L;
public SearchPanel(){
jta.setPreferredSize(new Dimension(120, 20));
add(jta);
jta.setBorder(null);
add(SearchButton);
SearchButton.addActionListener(this);
SearchButton.setBorder(null);
SearchButton.setContentAreaFilled(false);
SearchButton.setRolloverIcon(new ImageIcon("src//searchbutton2.png"));
SearchButton.setPressedIcon(new ImageIcon("src//searchbutton3.png"));
setVisible(true);
setBackground(null);
}
JTextArea jta=new JTextArea();
JButton SearchButton=new JButton(new ImageIcon("src//searchbutton1.png"));
public void actionPerformed(ActionEvent e){
if(jta.getText()!=null){
for(int i=0;i<PLAYER.SongModel.size();i++){
if(PLAYER.SongModel.get(i).indexOf(jta.getText())>=0||PLAYER.SongModel.get(i).toLowerCase().indexOf(jta.getText())>=0||PLAYER.SongModel.get(i).toUpperCase().indexOf(jta.getText())>=0){
PLAYER.SongList.setSelectedIndex(i);
}
}
}
}
}
整个程序其实就是由几个面板组成的,每个面板负责不同的功能。归根到底mp3播放器的编写其实主要是swing界面的边界,就是一堆按钮啊,列表啊和监听事件。。。
附上程序截图:
源程序网盘地址: