您好,登錄后才能下訂單哦!
java 實現音樂播放器的簡單實例
實現效果圖:
代碼如下
package cn.hncu.games; import java.applet.Applet; import java.applet.AudioClip; import java.awt.Color; import java.awt.Font; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.io.File; import java.net.URL; import javax.swing.DefaultListModel; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.ListModel; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; public class MusicPlayer extends JFrame{ //顯示(歌曲名稱)播放狀態的標簽 JLabel songNameLabel = null; //四個播放功能鍵按鈕 JButton btnLast = null; //上一曲 JButton btnPlay = null; //播放/停止 JButton btnNext = null; //下一曲 JButton btnLoop = null; //循環 //歌曲列表 JList songsList = null; AudioClip songs[] = null; AudioClip currentSong = null; int index=0; //當前歌曲在JList中的位置(序號) //歌曲文件名數組---String String[] strSongNames={ "song1.wav","song2.wav","song3.wav","song4.wav","song5.wav","song6.wav" }; final String DIR="songs\\"; //播放音樂的線程 Thread playerThread=null; boolean isPlayOrStop = true;//true代表播放狀態 boolean isLoop = false; //是否為循環狀態 public MusicPlayer() { super("音樂播放器"); setBounds(300, 50, 310, 500); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(null); //hello(); //顯示(歌曲名稱)播放狀態的標簽 songNameLabel = new JLabel(); Font songNameFont = new Font("黑體",Font.ITALIC,18); songNameLabel.setFont(songNameFont); songNameLabel.setText("我的音樂播放器"); songNameLabel.setBounds(10, 10, 300, 40); 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("images2/1.png")); btnPlay.setIcon( new ImageIcon("images2/2.png")); btnNext.setIcon( new ImageIcon("images2/3.png")); btnLoop.setIcon( new ImageIcon("images2/4.png")); //添加到框架 getContentPane().add(btnLast); getContentPane().add(btnPlay); getContentPane().add(btnNext); getContentPane().add(btnLoop); //添加監聽 MyMouseListener mml = new MyMouseListener(); btnLast.addMouseListener(mml); btnPlay.addMouseListener(mml); btnNext.addMouseListener(mml); btnLoop.addMouseListener(mml); //歌曲列表的標題 JLabel listLabel = new JLabel("播放列表"); listLabel.setBounds(10, 120, 100, 30); Font listLabelFont = new Font("黑體",Font.BOLD,16); listLabel.setFont(listLabelFont); getContentPane().add(listLabel); //歌曲列表 /* songsList = new JList(); songsList.setBounds(10, 150, 250, 300); songsList.setBackground(Color.CYAN); //把所有歌曲名逐個添加到List中 //songsList.setListData(strSongNames); for(int i=0;i<strSongNames.length;i++){ DefaultListModel dm = (DefaultListModel)songsList.getModel(); dm.add(i,strSongNames[i]); } getContentPane().add(songsList); */ DefaultListModel lm = new DefaultListModel(); songsList = new JList(lm); songsList.setBounds(10, 150, 250, 300); songsList.setBackground(Color.CYAN); //把所有歌曲名逐個添加到List中 //songsList.setListData(strSongNames); songs = new AudioClip[strSongNames.length]; for(int i=0;i<strSongNames.length;i++){ lm.add(i,strSongNames[i]); songs[i] = loadSound(strSongNames[i]); } getContentPane().add(songsList); //lm.remove(3); //對JList控件的監聽技術實現 songsList.addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { currentSong.stop(); index = songsList.getSelectedIndex(); isPlayOrStop = true; playerThread = new Thread( new MusicRun() ); playerThread.start(); } }); //單開一個線程,專用于播放音樂 playerThread = new Thread( new MusicRun() ); playerThread.start(); setVisible(true); } private AudioClip loadSound(String fileName) { try { URL url = new URL("file:songs\\"+fileName); AudioClip au = Applet.newAudioClip(url); return au; } catch (Exception e) { e.printStackTrace(); } return null; } //講解音樂播放的基本技術 private void hello() { try { File f = new File("songs\\song1.wav"); URL url = f.toURI().toURL(); //URL url = new URL("file:songs\\song1.wav"); AudioClip au = Applet.newAudioClip(url); au.play(); //au.loop(); //au.stop(); } catch (Exception e) { e.printStackTrace(); } } private class MyMouseListener extends MouseAdapter{ @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; }else if(btn==btnNext){ index++; index = index%strSongNames.length; }else if(btn==btnLoop){ isLoop = !isLoop; } if(isPlayOrStop){//播放 playerThread = new Thread( new MusicRun() ); playerThread.start(); }else{//停止 songsList.setSelectedIndex(index); songNameLabel.setText("停止播放:"+strSongNames[index]); btnPlay.setIcon( new ImageIcon("images2/2.png")); } } } private class MusicRun implements Runnable{ @Override public void run() { currentSong = songs[index]; if(isLoop){ currentSong.loop(); songNameLabel.setText("循環播放:"+strSongNames[index]); } if (isPlayOrStop) { currentSong.play(); } //在播放列表中選定當前歌曲 songsList.setSelectedIndex(index); //把播放按鈕的圖標切換成“停止” btnPlay.setIcon( new ImageIcon("images2/5.png")); if(!isLoop){ songNameLabel.setText("正在播放:"+strSongNames[index]); } } } public static void main(String[] args) { new MusicPlayer(); } }
如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望通過本文能幫助到大家,謝謝大家對本站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。