注意:按钮图片和音频文件(*.wav)需要自己去网上下载替换(这里不方便上传),有什么问题,可以留言。。。
import java.applet.Applet; import java.applet.AudioClip; import java.awt.Color; import java.awt.Font; import java.awt.List; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.net.MalformedURLException; import java.net.URL; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; public class MusicPlayer extends JFrame { JLabel songNameLabel = null; // 用标签显示状态 // 四个播放功能键按钮 JButton btnLast = null; // 上一曲 JButton btnPlay = null; // 播放/停止 JButton btnNext = null; // 下一曲 JButton btnLoop = null; // 循环 // 显示歌曲列表 JLabel listLabel = null; List songsList = null; AudioClip[] songs;// 将音频文件放在数组当中 AudioClip currentSong;// 当前选定播放的音频文件 // 本例,此处把歌曲名写死了。同学们自己可以进行扩展:由用户从文件对话框动态加载 String[] strSongNames = { "song1.wav", "song2.wav", "song3.wav", "song4.wav", "song5.wav", "song6.wav" }; String strCurrentSongName; // 当前选定播放歌曲的名称 int index = 0; // 记录当前选定播放的歌曲的序号(当前播放的那首歌曲),默认第一首 boolean isPlayOrStop = true;// 记录播放状态(播放/停止),默认为播放 boolean isLoop = false; // 记录循环状态,默认为不循环播放 Thread playerThread; // 音频播放线程 public MusicPlayer() { super("音乐播放器"); this.setLayout(null); this.setBounds(300, 50, 310, 500); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 标签:我的音乐播放器/歌曲名 songNameLabel = new JLabel(); Font songNameFont = new Font("斜体", Font.ITALIC, 18); songNameLabel.setFont(songNameFont); songNameLabel.setText("我的音乐播放器"); songNameLabel.setBounds(10, 10, 300, 40); this.getContentPane().add(songNameLabel); // 四个播放功能键按钮 // 构造四个按钮 btnLast = new JButton(); btnPlay = new JButton(); btnNext = new JButton(); btnLoop = new JButton(); // 设置位置和大小 btnLast.setBounds(10, 70, 50, 40); btnPlay.setBounds(70, 70, 50, 40); btnNext.setBounds(130, 70, 50, 40); btnLoop.setBounds(190, 70, 50, 40); // 设置图片 btnLast.setIcon(new ImageIcon("1.png")); btnPlay.setIcon(new ImageIcon("2.png")); btnNext.setIcon(new ImageIcon("3.png")); btnLoop.setIcon(new ImageIcon("4.png")); // 给按钮添加监听 MouseListener mlc = new MouseListenerC();// 鼠标监听器对象 btnLast.addMouseListener(mlc); btnPlay.addMouseListener(mlc); btnNext.addMouseListener(mlc); btnLoop.addMouseListener(mlc); // 添加到框架 getContentPane().add(btnLast); getContentPane().add(btnPlay); getContentPane().add(btnNext); getContentPane().add(btnLoop); // 以下这段设置歌曲播放列表 // 设置播放列表的标签 listLabel = new JLabel("播放列表"); listLabel.setBounds(10, 120, 100, 30); Font listLabelFont = new Font("宋体", Font.BOLD, 15); listLabel.setFont(listLabelFont); this.getContentPane().add(listLabel); // 设置播放列表控件 songsList = new List(); songsList.setBounds(10, 150, 250, 300); songsList.setBackground(Color.cyan); songs = new AudioClip[10]; // 把歌曲根据文件名逐个加载进来 for (int i = 0; i < strSongNames.length; i++) { songs[i] = loadSound(strSongNames[i]);// 加载音频文件 songsList.add(strSongNames[i]);// 将歌曲名称添加到列表中 } songsList.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { currentSong.stop(); index = songsList.getSelectedIndex(); playerThread = new Thread( new MusicRun() ); playerThread.start(); } }); this.getContentPane().add(songsList); playerThread = new Thread( new MusicRun() ); playerThread.start(); this.setVisible(true); } private class MusicRun implements Runnable { @Override public void run() { currentSong = songs[index]; if (isLoop) { currentSong.loop(); } strCurrentSongName = strSongNames[index]; songNameLabel.setText("正在播放:" + strCurrentSongName); songsList.select(index);// 在播放列表中选定当前播放歌曲条目 btnPlay.setIcon(new ImageIcon("5.png"));// 切换成“停止”图标 if (isPlayOrStop) { currentSong.play(); } } } private class MouseListenerC implements MouseListener { @Override public void mouseClicked(MouseEvent e) { JButton btn = (JButton) e.getSource(); currentSong.stop(); if (btn == btnPlay) { isPlayOrStop = !isPlayOrStop; } else if (btn == btnLast) { index--; if (index < 0) { index = strSongNames.length - 1; } isPlayOrStop = true; isLoop = false; } else if (btn == btnNext) { index++; index = index % strSongNames.length; isPlayOrStop = true; isLoop = false; } else if (btn == btnLoop) { isLoop = !isLoop; } if (isPlayOrStop) { playerThread = new Thread(new MusicRun() ); playerThread.run(); } else { songNameLabel.setText("停止播放:" + strCurrentSongName); btnPlay.setIcon(new ImageIcon("2.png")); } } @Override public void mousePressed(MouseEvent e) { } @Override public void mouseReleased(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } } public AudioClip loadSound(String filename) { URL url = null; try { url = new URL("file:" + filename); } catch (MalformedURLException e) { e.printStackTrace(); } return Applet.newAudioClip(url); } public static void main(String[] args) { MusicPlayer mp = new MusicPlayer(); } }