package musicplayer;
import java.applet.Applet;
import java.applet.AudioClip;
import java.net.MalformedURLException;
import java.net.URL;
import java.io.*;
import javax.swing.text.*;
import javax.swing.text.StyleContext.NamedStyle;
import javax.swing.*;
import static javax.swing.JFrame.*; //引入JFrame的静态常量
import java.awt.event.*;
import java.awt.*;
import java.net.*;
import java.util.*;
import java.util.Timer;
@SuppressWarnings("removal")
class audioplay{//播放音乐类
AudioClip adc;// 声音音频剪辑对象
URL url;
boolean adcFlag=false;
boolean playFlag=false;
void SetPlayAudioPath(String path){
try{
url = new URL(path);
// System.out.println(adc.toString());
if(adcFlag==true){adc.stop();playFlag=false;}
adc = Applet.newAudioClip(url);
adcFlag=true;
}
catch (MalformedURLException e1) {
e1.printStackTrace();
}
}
void play(){ //开始播放
adc.play();
playFlag=true;
}
void stop(){ //停止播放
adc.stop();
playFlag=false;
}
}
class MyExtendsJFrame extends JFrame implements ActionListener,MouseListener { //窗口类
String playFile;
String playFileName;
String playFileDirectory;
JLabel background;//背景控件
JLabel background0;//背景控件
JLabel background1;//背景控件
JLabel background2;//背景控件
JLabel background3;//背景控件
JButton buttonPlay;//播放按钮
JButton buttonNext;
JButton buttonCircle;
JButton buttonvoice;
JButton buttonPre;
JButton buttonOpenFile;
JButton buttonShowList;
JTextPane textLyrics;//歌词控件
JLabel playTime;//播放进度条控件
JList listPlayFile;//播放列表控件
JList listPlayFileTime;
Timer nTimer;//定时器对象
JTextArea playTimeCount;//播放器时间显示
JButton ShowList;//显示列表按钮
JLabel picture_gif;//播放动画
JTextArea mname;//歌名
audioplay audioPlay;
int iCurFileTime;
public MyExtendsJFrame(){
setTitle("E02014275蔡沐恩播放器");//软件名
audioPlay = new audioplay();
setBounds(160,100,1100,600); //设置窗口大小
setLayout(null);//空布局
init(); //添加控件的操作封装成一个函数
setVisible(true);//放在添加组件后面执行
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
void init(){//添加的控件
Icon img=new ImageIcon("C:/Users/www11/eclipse-workspace/musicplayer/background1.jpg"); //创建图标对象
background = new JLabel(img);//设置背景图片
background.setBounds(0,0,1100,900);//设置背景控件大小
getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));//背景图片控件置于最底层
((JPanel)getContentPane()).setOpaque(false); //控件透明
Icon img7=new ImageIcon("C:/Users/www11/eclipse-workspace/musicplayer/222.gif"); //创建图标对象
background0 = new JLabel(img7);//设置背景图片
background0.setBounds(0,75,100,100);//设置背景控件大小
getLayeredPane().add(background0, new Integer(Integer.MIN_VALUE+2));//背景图片控件置于最底层
((JPanel)getContentPane()).setOpaque(false); //控件透明
Icon img2=new ImageIcon("C:/Users/www11/eclipse-workspace/musicplayer/1.jpg"); //创建图标对象
playTime = new JLabel(img2); //创建播放进度条对象
playTime.setBounds(0,446,1100,100); //设置播放进度条对象大小
getLayeredPane().add(playTime, new Integer(Integer.MIN_VALUE+3));
((JPanel)getContentPane()).setOpaque(false); //控件透明
Icon img3=new ImageIcon("C:/Users/www11/eclipse-workspace/musicplayer/background2.jpg"); //创建图标对象
background1 = new JLabel(img3);//设置背景图片
background1.setBounds(0,645,1139,100);//设置背景控件大小
getLayeredPane().add(background1, new Integer(Integer.MIN_VALUE+1));//背景图片控件置于最底层
((JPanel)getContentPane()).setOpaque(false); //控件透明
Icon img4=new ImageIcon("C:/Users/www11/eclipse-workspace/musicplayer/background3.jpg"); //创建图标对象
background2 = new JLabel(img4);//设置背景图片
background2.setBounds(0,-5,1139,80);//设置背景控件大小
getLayeredPane().add(background2, new Integer(Integer.MIN_VALUE+1));//背景图片控件置于最底层
((JPanel)getContentPane()).setOpaque(false); //控件透明
Icon img5=new ImageIcon("C:/Users/www11/eclipse-workspace/musicplayer/background.jpg"); //创建图标对象
background3 = new JLabel(img5);//设置背景图片
background3.setBounds(170,100,250,116);//设置背景控件大小
getLayeredPane().add(background3, new Integer(Integer.MIN_VALUE+1));//背景图片控件置于最底层
((JPanel)getContentPane()).setOpaque(false); //控件透明
buttonPlay=new JButton();//添加播放按钮
buttonPlay.setBounds(518,499,64,64); //设置播放按钮大小
Icon icon=new ImageIcon("C:/Users/www11/eclipse-workspace/musicplayer/play.png");//创建播放图标对象
buttonPlay.setIcon(icon); //设置播放图标
buttonPlay.setBorderPainted(false);
buttonPlay.setFocusPainted(false);
getLayeredPane().add(buttonPlay, new Integer(Integer.MIN_VALUE+2));
this.buttonPlay.addActionListener(this);
//add(buttonPlay);//添加播放按钮至窗口中
buttonNext = new JButton();//添加播放按钮
buttonNext.setBounds(582,499,64,64); //设置播放按钮大小
Icon icon1=new ImageIcon("C:/Users/www11/eclipse-workspace/musicplayer/next.png");//创建播放图标对象
buttonNext.setIcon(icon1); //设置播放图标
buttonNext.setBorderPainted(false);
buttonNext.setFocusPainted(false);
getLayeredPane().add(buttonNext, new Integer(Integer.MIN_VALUE+2));
this.buttonNext.addActionListener(this);
buttonCircle = new JButton();//添加播放按钮
buttonCircle.setBounds(454,499,64,64); //设置播放按钮大小
Icon icon3=new ImageIcon("C:/Users/www11/eclipse-workspace/musicplayer/circle.png");//创建播放图标对象
buttonCircle.setIcon(icon3); //设置播放图标
buttonCircle.setBorderPainted(false);
buttonCircle.setFocusPainted(false);
getLayeredPane().add(buttonCircle, new Integer(Integer.MIN_VALUE+2));
this.buttonCircle.addActionListener(this);
buttonvoice = new JButton();//添加播放按钮
buttonvoice.setBounds(950,499,64,64); //设置播放按钮大小
Icon icon4=new ImageIcon("C:/Users/www11/eclipse-workspace/musicplayer/voice.png");//创建播放图标对象
buttonvoice.setIcon(icon4); //设置播放图标
buttonvoice.setBorderPainted(false);
buttonvoice.setFocusPainted(false);
getLayeredPane().add(buttonvoice, new Integer(Integer.MIN_VALUE+2));
buttonPre = new JButton();//添加播放按钮
buttonPre.setBounds(390,499,64,64); //设置播放按钮大小
Icon icon2=new ImageIcon("C:/Users/www11/eclipse-workspace/musicplayer/pre.png");//创建播放图标对象
buttonPre.setIcon(icon2); //设置播放图标
buttonPre.setBorderPainted(false);
buttonPre.setFocusPainted(false);
getLayeredPane().add(buttonPre, new Integer(Integer.MIN_VALUE+2));
this.buttonPre.addActionListener(this);
buttonShowList = new JButton();//添加播放按钮
buttonShowList.setBounds(1014,499,64,64); //设置播放按钮大小
Icon icon5=new ImageIcon("C:/Users/www11/eclipse-workspace/musicplayer/list.png");//创建播放图标对象
buttonShowList.setIcon(icon5); //设置播放图标
buttonShowList.setBorderPainted(false);
getLayeredPane().add(buttonShowList, new Integer(Integer.MIN_VALUE+2));
this.buttonShowList.addActionListener(this);
buttonOpenFile = new JButton();//添加播放按钮
buttonOpenFile.setBounds(864,499,64,64); //设置播放按钮大小
Icon icon6=new ImageIcon("C:/Users/www11/eclipse-workspace/musicplayer/baseball.png");//创建播放图标对象
buttonOpenFile.setIcon(icon6); //设置播放图标
buttonOpenFile.setBorderPainted(false);
getLayeredPane().add(buttonOpenFile, new Integer(Integer.MIN_VALUE+2));
this.buttonOpenFile.addActionListener(this);
listPlayFile=new JList(); //创建播放列表
listPlayFile.setBounds(600,200,400,150); //设置播放列表大小
listPlayFile.setOpaque(false);//设置播放列表透明
listPlayFile.setBackground(new Color(0, 0, 0, 0));//设置播放列表背景颜色
listPlayFile.setForeground(Color.red);//设置播放列表字体颜色
Font f = new Font("宋体",Font.ITALIC,20);
listPlayFile.setFont(f);
add(listPlayFile);
listPlayFile.addMouseListener(this);
listPlayFileTime=new JList();
listPlayFileTime.setBounds(1004,200,50,150);
listPlayFileTime.setOpaque(false);
listPlayFileTime.setBackground(new Color(0,0,0,0));
listPlayFileTime.setForeground(Color.BLACK);
listPlayFileTime.setFont(f);
add(listPlayFileTime);
textLyrics=new JTextPane(); //创建歌词控件
textLyrics.setBounds(100,250,400,300);//设置歌词控件大小
textLyrics.setForeground(Color.black);//歌词控件字体颜色
Font f1 = new Font("正楷",Font.ITALIC,20);
textLyrics.setFont(f1);
textLyrics.setOpaque(false);//歌词控件透明
add(textLyrics); //添加歌词控件至窗口中
playTimeCount=new JTextArea();//当前时间
playTimeCount.setBounds(50,500,100,100);
playTimeCount.setForeground(Color.black);
playTimeCount.setFont(f1);
playTimeCount.setOpaque(false);
getLayeredPane().add(playTimeCount, new Integer(Integer.MIN_VALUE+3));
playTimeCount.setText("0:00");
mname=new JTextArea();//当前歌曲名称
mname.setBounds(200,140,250,100);
mname.setForeground(Color.red);
Font f3 = new Font("幼圆",Font.ITALIC,25);
mname.setFont(f3);
mname.setOpaque(false);
add(mname);
}
public void timerFun(int musicTime){//定时器函数
this.iCurFileTime = musicTime;
if (this.nTimer != null)
this.nTimer.cancel();
this.nTimer = new Timer();
this.nTimer.schedule(new TimerTask() {
int nPlayTime = 0;
int n=0;
public void run() {
String str1 , str2;
this.nPlayTime++;
if (this.nPlayTime >= MyExtendsJFrame.this.iCurFileTime)
MyExtendsJFrame.this.nTimer.cancel();
int iSecond = this.nPlayTime % 60;
int iMinute = this.nPlayTime / 60;
int j = iSecond;
int k = iMinute;
if (iSecond < 10)
str1 = "0" + iSecond;
else
str1 = String.valueOf(iSecond);
if (iMinute < 10)
str2 = "0" + iMinute;
else
str2=String.valueOf(iMinute);
MyExtendsJFrame.this.playTimeCount.setText(String.valueOf(str2) + ":" + String.valueOf(str1));
MyExtendsJFrame.this.playTime.setBounds(125, 650, 700 * this.nPlayTime / MyExtendsJFrame.this.iCurFileTime, 3);
String[] sLyrics = {
"我和我的祖国,一刻也不能分割\r\n"
, "无论我走到哪里,都流出一首赞歌\r\n"
, "我歌唱每一座高山,我歌唱每一条河\r\n"
, "袅袅炊烟,小小村落,路上一道辙\r\n"
};
MyExtendsJFrame.this.textLyrics.setText("");
for (int i = 0; i < sLyrics.length; i++) {
SimpleAttributeSet attrSet = new SimpleAttributeSet();
StyleConstants.setFontFamily(attrSet, "5");
try {
Document doc = MyExtendsJFrame.this.textLyrics.getDocument();
StyleConstants.setForeground(attrSet, Color.black);
StyleConstants.setBold(attrSet, true);
StyleConstants.setFontSize(attrSet, 20);
if (this.nPlayTime >= 17 && this.nPlayTime <= 20 && i == 0) {
doc.insertString(doc.getLength(), sLyrics[i], attrSet);
} else if (this.nPlayTime >= 21 && this.nPlayTime <= 24 && i == 1) {
doc.insertString(doc.getLength(), sLyrics[i], attrSet);
} else if (this.nPlayTime >= 25 && this.nPlayTime <= 29 && i == 2) {
doc.insertString(doc.getLength(), sLyrics[i], attrSet);
} else if (this.nPlayTime >= 30 && this.nPlayTime <= 34 && i == 3) {
doc.insertString(doc.getLength(), sLyrics[i], attrSet);
} else if (this.nPlayTime >= 35 && this.nPlayTime <= 38 && i == 4) {
doc.insertString(doc.getLength(), sLyrics[i], attrSet);
} else if (this.nPlayTime >= 39 && this.nPlayTime <= 43 && i == 5) {
doc.insertString(doc.getLength(), sLyrics[i], attrSet);
} else if (this.nPlayTime >= 44 && this.nPlayTime <= 48 && i == 6) {
doc.insertString(doc.getLength(), sLyrics[i], attrSet);
} else if (this.nPlayTime >= 49 && this.nPlayTime <= 52 && i == 7) {
doc.insertString(doc.getLength(), sLyrics[i], attrSet);
} else if (this.nPlayTime >= 53 && this.nPlayTime <= 58 && i == 8) {
doc.insertString(doc.getLength(), sLyrics[i], attrSet);
} else if (this.nPlayTime >= 59 && this.nPlayTime <= 65 && i == 9) {
doc.insertString(doc.getLength(), sLyrics[i], attrSet);
} else if (this.nPlayTime >= 66 && this.nPlayTime <= 71 && i == 10) {
doc.insertString(doc.getLength(), sLyrics[i], attrSet);
} else if (this.nPlayTime >= 72 && this.nPlayTime <= 75 && i == 11) {
doc.insertString(doc.getLength(), sLyrics[i], attrSet);
} else if (this.nPlayTime >= 76 && this.nPlayTime <= 80 && i == 12) {
doc.insertString(doc.getLength(), sLyrics[i], attrSet);
} else if (this.nPlayTime >= 81 && this.nPlayTime <= 86 && i == 13) {
doc.insertString(doc.getLength(), sLyrics[i], attrSet);
} else if (this.nPlayTime >= 87 && this.nPlayTime <= 93 && i == 14) {
doc.insertString(doc.getLength(), sLyrics[i], attrSet);
} else if (this.nPlayTime >= 94 && this.nPlayTime <= 100 && i == 15) {
doc.insertString(doc.getLength(), sLyrics[i], attrSet);
} else {
StyleConstants.setForeground(attrSet, Color.white);
StyleConstants.setBold(attrSet, false);
StyleConstants.setFontSize(attrSet, 25);
doc.insertString(doc.getLength(), sLyrics[i], attrSet);
}
} catch (BadLocationException badLocationException) {}
}
}
},0,1000);
}
public void mouseClicked (MouseEvent e){
if (e.getClickCount() == 2) {
if (e.getSource() == this.listPlayFile) {
int len = this.listPlayFile.getModel().getSize();
if (len != 0) {
if (this.listPlayFile.getSelectedValue() != null) {
this.playFile = String.valueOf(this.playFileDirectory) + this.listPlayFile.getSelectedValue().toString();
} else {
this.playFile = String.valueOf(this.playFileDirectory) + this.listPlayFile.getModel().getElementAt(0).toString();
}
this.audioPlay.SetPlayAudioPath("file:" + this.playFile);
this.audioPlay.play();
this.mname.setText(this.listPlayFile.getSelectedValue().toString());
Icon icon = new ImageIcon("C:/Users/www11/eclipse-workspace/musicplayer/stop.png");
this.buttonPlay.setIcon(icon);
File file = new File(this.playFile);
int nMusicTime = (int) file.length() / 1024 / 173;
timerFun(nMusicTime);
}
}
}
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
@SuppressWarnings("unchecked")//忽略警告
public void actionPerformed(ActionEvent e){
if (e.getSource() == this.buttonPlay)
if (!this.audioPlay.playFlag) {
int len = this.listPlayFile.getModel().getSize();
if (len != 0) {
if (this.listPlayFile.getSelectedValue() != null) {
this.playFile = String.valueOf(this.playFileDirectory) + this.listPlayFile.getSelectedValue().toString();
} else {
this.playFile = String.valueOf(this.playFileDirectory) + this.listPlayFile.getModel().getElementAt(0).toString();
this.listPlayFile.setSelectedIndex(0);
}
this.audioPlay.SetPlayAudioPath("file:" + this.playFile);
this.audioPlay.play();
this.mname.setText(this.listPlayFile.getSelectedValue().toString());
Icon icon = new ImageIcon("C:/Users/www11/eclipse-workspace/musicplayer/stop.png");
this.buttonPlay.setIcon(icon);
File file = new File(this.playFile);
int nMusicTime = (int)file.length() / 1024 / 173;
timerFun(nMusicTime);
}
} else {
this.audioPlay.stop();
Icon icon = new ImageIcon("C:/Users/www11/eclipse-workspace/musicplayer/play.png");
this.buttonPlay.setIcon(icon);
this.nTimer.cancel();
}
if (e.getSource() == this.buttonShowList)
if (this.listPlayFile.isVisible()) {
this.listPlayFile.setVisible(false);
this.listPlayFileTime.setVisible(false);
} else {
this.listPlayFile.setVisible(true);
this.listPlayFileTime.setVisible(true);
}
if (e.getSource() == this.buttonOpenFile) {
FileDialog openFile = new FileDialog(this, "");
openFile.setVisible(true);
this.playFileName = openFile.getFile();
this.playFileDirectory = openFile.getDirectory();
this.playFile = String.valueOf(this.playFileDirectory) + this.playFileName;
this.audioPlay.SetPlayAudioPath("file:" + this.playFile);
this.audioPlay.stop();
System.out.println(this.playFile);
Vector<String> vt = new Vector();
int len = this.listPlayFile.getModel().getSize();
String[] listString = new String[len];
for (int i = 0; i < len; i++) {
listString[i] = this.listPlayFile.getModel().getElementAt(i).toString();
vt.add(listString[i]);
}
vt.add(this.playFileName);
this.listPlayFile.setListData(vt);
Vector<String> vtime = new Vector();
len = this.listPlayFileTime.getModel().getSize();
for (int j = 0; j < len; j++) {
listString[j] = this.listPlayFileTime.getModel().getElementAt(j).toString();
vtime.add(listString[j]);
}
File file = new File(this.playFile);
int iMusicTime = (int)file.length() / 1024 / 173;
int iSecond = iMusicTime % 60;
int iMinute = iMusicTime / 60;
vtime.add(String.valueOf(iMinute) + ":" + iSecond);
this.listPlayFileTime.setListData(vtime);
this.audioPlay.SetPlayAudioPath("file:" + this.playFile);
this.audioPlay.play();
this.mname.setText(this.playFileName);
Icon icon = new ImageIcon("C:/Users/www11/eclipse-workspace/musicplayer/stop.png");
this.buttonPlay.setIcon(icon);
int nMusicTime = (int)file.length() / 1024 / 173;
timerFun(nMusicTime);
}
}
}
public class MusicPlay1{
@SuppressWarnings("unchecked")//忽略警告
public static void main(String[] args) {
audioplay audioPlay=new audioplay(); //创建播放对象
MyExtendsJFrame frame=new MyExtendsJFrame();//创建聊天程序窗口
}
}
实验是ahu university下的java实验课要求设计一个音乐播放器。
用java实现音乐播放器musicplayer,其中音乐播放器包含进度条,音乐歌词,播放停止快进按钮,同时可以通过文件选择对音乐播放文件进行筛选,创建时间类,可以通过对音乐列表的点击对音乐进行切换播放...