自制简易音乐播放器 我的Java学习之路


package MusicPlay;



import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;


import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
public class MusicPlay extends JFrame {
private String[] songsName,songAbb;
private String dirSongs="src/MusicPlay/songs/";
private String dirImages="src/MusicPlay/images/";
private JButton btnLast,btnPlay,btnNext,btnLoop;
private JLabel lbNowPlay;
private List songsList;
private boolean isPlay=true,isLoop;
private AudioClip currentsong=null;
private AudioClip[] songs;
private int index;//播放序号;
private Thread playThread;
public MusicPlay(){
setBounds(300, 50, 300, 500);
setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);

setBackGroud();
lbNowPlay=new JLabel("正在播放:");
lbNowPlay.setFont(new Font("a",Font.BOLD,25));
lbNowPlay.setBounds(10, 10, 300, 40);
getContentPane().add(lbNowPlay);
//四个播放按钮
//构造
btnLast = new JButton(new ImageIcon(dirImages+"last.jpg"));
btnPlay = new JButton();
btnPlay.setIcon(new ImageIcon(dirImages+"play.jpg"));
btnNext = new JButton(new ImageIcon(dirImages+"next.jpg"));
btnLoop = new JButton(new ImageIcon(dirImages+"loop.jpg"));
//设置大小
btnLast.setBounds(20, 70, 40, 40);
btnPlay.setBounds(75,65, 50, 50);
btnLoop.setBounds(200, 70, 50, 40);
btnNext.setBounds(140, 70, 50, 40);


getContentPane().add(btnLast);
getContentPane().add(btnPlay);
getContentPane().add(btnNext);
getContentPane().add(btnLoop);


//监听
Listener listener=new Listener();//鼠标监听有特殊效果
btnLast.addMouseListener(listener);
btnPlay.addMouseListener(listener);
btnNext.addMouseListener(listener);
btnLoop.addMouseListener(listener);


//播放列表
//设置播放列表的标签
JLabel listLabel = new JLabel("播放列表");
listLabel.setBounds(10, 120, 100, 30);
Font listLabelFont = new Font("宋体",Font.BOLD,15);
listLabel.setFont(listLabelFont);
getContentPane().add(listLabel);
songsList=new List();
songsList.setBounds(10, 150, 250, 300);
songsList.setBackground(Color.green);
getContentPane().add(songsList);
getSongsName();//获取歌曲名
songAbb=new String[songsName.length];
for(int i=0;i<songsName.length;i++){
songAbb[i]=InitilName(songsName[i]);
}
songs=new AudioClip[songsName.length];
for(int i=0;i<songs.length;i++){
songs[i]=loadSong(songsName[i]);
songsList.add(songAbb[i]);
}


songsList.addItemListener((new ItemListener() {


@Override
public void itemStateChanged(ItemEvent e) {
currentsong.stop();
index = songsList.getSelectedIndex();
playThread = new Thread(new MusicRun());
playThread.start();
}
}));
playThread = new Thread(new MusicRun());
playThread.start();
setVisible(true);
}
private void setBackGroud() {
((JPanel)(this.getContentPane())).setOpaque(false);
Icon bgImage=new ImageIcon(dirImages+"a.gif");
JLabel lbBack=new JLabel(bgImage);
this.getLayeredPane().add(lbBack,new Integer(Integer.MIN_VALUE));
lbBack.setBounds(0, 0, bgImage.getIconWidth(), bgImage.getIconHeight());
}
private String InitilName(String str){
int i=str.indexOf('.');
String s=str.substring(0,i);


return s;
}
private AudioClip loadSong(String fileName) {
AudioClip song=null;
try {
URL url=new URL("file:"+dirSongs+fileName);
song=Applet.newAudioClip(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
return song;
}


public static void main(String[] args) {
MusicPlay m=new MusicPlay();
}


private void getSongsName() {
File fdir=new File(dirSongs);
File f[]=fdir.listFiles();
songsName=new String[f.length];
int i=0;
for(File m:f){
songsName[i++]=m.getName();
}


}


private class MusicRun implements Runnable{


@Override
public void run() {
currentsong=songs[index];
currentsong.play();
if(isLoop){
currentsong.loop();
}
songsList.select(index);
lbNowPlay.setText("正在播放:"+songAbb[index]);
}


}
private class Listener implements MouseListener{




@Override
public void mouseClicked(MouseEvent e) {
JButton btn=(JButton)e.getSource();
if(btn==btnPlay){
isPlay=!isPlay;
}else if(btn==btnNext){
index=(index+1)%songs.length;
isPlay=true;
}else if(btn==btnLast){
index=(index-1+songs.length)%songs.length;
isPlay=true;
}else if(btn==btnLoop){
isLoop = !isLoop;
}
if(isPlay==true){//如果一个等号,就是赋值
currentsong.stop();
playThread = new Thread(new MusicRun());
playThread.start();
btnPlay.setIcon(new ImageIcon(dirImages+"play.jpg"));
}else{
currentsong.stop();
lbNowPlay.setText("继续播放:"+songAbb[index]);
btnPlay.setIcon(new ImageIcon(dirImages+"stop.jpg"));
}


}


@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


}


}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值