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
}
}