簡單音樂播放器----java (美。。。。)

YX_blog發表於2015-06-18

package cn.hncu.games;


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;
import javax.swing.JList;


public class Musicplayer extends JFrame implements MouseListener {


JLabel songStatus;
JButton btlast;
JButton btplay;
JButton btNext;
JButton btloop;
List songList;
boolean  isLoop=false;
int index=0;
String str;
boolean isPlay=true;




String[] strSongNames = { "song1.wav", "song2.wav", "song3.wav",
"song4.wav", "song5.wav", "song6.wav" };
AudioClip[] songs;
AudioClip currentSong;
Thread mythread;




public Musicplayer() {
super("音樂播放器");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setBounds(300, 50, 310, 500);
this.setLayout(null);//自己定義佈局,去掉原先的佈局;
//顯示歌曲播放的狀態;
songStatus =new JLabel("播放歌曲");
songStatus.setFont(new Font(" ",Font.BOLD, 20));
songStatus.setBounds(10, 10, 300, 40);
this.getContentPane().add(songStatus);
//四個按鍵;
btlast =new JButton(new ImageIcon("F:\\ex\\javamusic\\1.png"));
btplay =new JButton(new ImageIcon("F:\\ex\\javamusic\\5.png"));
btNext =new JButton(new ImageIcon("F:\\ex\\javamusic\\3.png"));
btloop =new JButton(new ImageIcon("F:\\ex\\javamusic\\4.png"));


btlast.setBounds(10, 70, 50, 40);
btplay.setBounds(70, 70, 50, 40);
btNext.setBounds(130, 70, 50, 40);
btloop.setBounds(190, 70, 50, 40);


this.getContentPane().add(btlast);
this.getContentPane().add(btplay);
this.getContentPane().add(btNext);
this.getContentPane().add(btloop);


btlast.addMouseListener(this);
btplay.addMouseListener(this);
btNext.addMouseListener(this);
btloop.addMouseListener(this);


// 選項的標籤
JLabel jb2=new JLabel("音樂選項");
jb2.setBounds(10, 120, 100, 30);
this.getContentPane().add(jb2);


//選項
songList =new List();
songList.setBounds(10, 150, 250, 300);
songList.setBackground(Color.blue);
this.getContentPane().add(songList);




songs=new AudioClip[8];
for(int i=0;i<strSongNames.length;i++){
songs[i]=loadSound(strSongNames[i]);
songList.add(strSongNames[i]);
}
songList.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
currentSong.stop();//當Jlist 裡面的選項選中的時候,當前的停止
index=songList.getSelectedIndex();//獲得選中的序號
mythread =new Thread(new musicthread());//設定啟動執行緒,還讓物件實現
str =strSongNames[index];
songStatus.setText(str);
mythread.start();


}
});
mythread =new Thread(new musicthread());//執行緒執行;
mythread.start();
this.setVisible(true);
}
class musicthread implements Runnable{
public void run(){
currentSong =songs[index];
if(isLoop){
currentSong.loop();
}
songStatus.setText(strSongNames[index]);
songList.select(index);
currentSong.play();
}
}


public static void main(String[] args) {
new Musicplayer();
// AudioClip av=mc.loadSound("F:\\ex\\javamusic\\2.wav");
// av.play();//本地測試用到
}
private AudioClip loadSound(String name) {//快速定位音訊資源
URL url=null;
try {
url=new URL("file:"+name);
} catch (MalformedURLException e) {
e.printStackTrace();
}
return Applet.newAudioClip(url);




}
@Override
public void mouseClicked(MouseEvent e) {
currentSong.stop();
if(e.getSource() == btNext){
index++;
if(index>5){
index=index%6;
}
currentSong =songs[index];
currentSong.play();
songStatus.setText(strSongNames[index]);
btplay.setIcon(new ImageIcon("F:\\ex\\javamusic\\5.png"));
if(isLoop){
currentSong.loop();
}
}
else if(e.getSource() == btlast){
//currentSong.stop();
btplay.setIcon(new ImageIcon("F:\\ex\\javamusic\\5.png"));
index--;
if(index<0){
index=(index+6);
}
currentSong =songs[index];
currentSong.play();
songStatus.setText(strSongNames[index]);
if(isLoop){
currentSong.loop();
}}
else if(e.getSource() == btplay){

isPlay=!isPlay;

if(isPlay){
currentSong.stop();
btplay.setIcon(new ImageIcon("F:\\ex\\javamusic\\2.png"));
songStatus.setText("播放停止"+strSongNames[index]);
}else{
btplay.setIcon(new ImageIcon("F:\\ex\\javamusic\\5.png"));
currentSong.play();
songStatus.setText(strSongNames[index]);
}

}else if(e.getSource()==btloop){
if(isLoop){
isLoop=!isLoop;//迴圈改變;
}
currentSong.play();
}
songList.select(index);//選中當前的播放歌曲


}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub


}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub


}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub


}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub


}


}

相關文章