第一个类:主要实现对音乐的播放及停止
public class PlayMusic {
public static Player playMusic(String MusicPath){
javax.media.Player player = null;
try {
if(null==player){
File musicFile = new File(MusicPath);
if(musicFile.exists()){
MediaLocator locator=new MediaLocator("file:"+musicFile.getAbsolutePath());
player = Manager.createRealizedPlayer(locator);
player.prefetch();// 预读文件
}else{
System.err.println(musicFile+" 找不到");
}
}
player.start();//播放
} catch (CannotRealizeException ex) {
ex.printStackTrace();
} catch (NoPlayerException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
return player;
}
public static void stopMusic(Player player){
try{
player.stop();
}catch(Exception e){
e.printStackTrace();
}
}
}
第二个类:主要实现界面的展示以及对事件的监听
public class PlayerFrame extends JFrame{
//对JMenuBar的初始化
private void jmenubarSet(){
jmb = new JMenuBar();
}
//对JMenu的初始化
private void jmenuSet(){
jm = new JMenu("文件");
jmb.add(jm);
}
//对JMenuItem的初始化
private void jmenuitemSet(){
jmi_open = new JMenuItem("打开");
jmi_stop = new JMenuItem("停止");
jm.add(jmi_open);
jm.add(jmi_stop);
}
//对JList的初始化
private void jlistSet(){
jl = new JList();
}
//对JTextArea的初始化
private void jtextSet(){
jta = new JTextArea();
}
//对JPanel及JscrollPane的初始化
private void panelSet(){
jspl = new JScrollPane(jl);
jspr = new JScrollPane(jta);
jspl.setPreferredSize(new Dimension(200,450));
jspr.setPreferredSize(new Dimension(200,450));
}
//设置窗体居中
private void setCenter(JFrame jf){
Toolkit tk =Toolkit.getDefaultToolkit();
Dimension dm = tk.getScreenSize();
jf.setLocation(dm.width/2-jf.getWidth()/2, dm.height/2-jf.getHeight()/2);
}
//得到歌曲对应的歌词路径
private static String getLrcName(String path){
String lrcFileName = path.substring(0,path.lastIndexOf(".")+1);
lrcFileName = lrcFileName + "lrc";
return lrcFileName;
}
//在JTextArea区域显示歌词
private static void getLrcContxt(String str_filePath,JTextArea ja) throws Exception{
BufferedReader bufferedReader = new BufferedReader(new FileReader(str_filePath));
String str_line;
ja.setText("");
while((str_line=bufferedReader.readLine())!=null){
if(flag_contxt){
ja.setText(str_line);
flag_contxt = false;
}
else{
ja.setText(ja.getText()+"\n"+str_line);
}
}
bufferedReader.close();
}
//构造函数
private PlayerFrame(){
jmenubarSet();
jmenuSet();
jmenuitemSet();
jlistSet();
jtextSet();
panelSet();
setLayout(new BorderLayout());
add(jmb,BorderLayout.NORTH);
add(jspl,BorderLayout.WEST);
add(jspr,BorderLayout.EAST);
setResizable(false);
setTitle("Player");
setVisible(true);
setSize(500,600);
setCenter(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
//JMenuItem的监听
jmi_open.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//如果存在播放的歌曲,停止播放
if(tempPlayer!=null)
PlayMusic.stopMusic(tempPlayer);
JFrame jf = new JFrame();
FileDialog fd = new FileDialog(jf,"选择歌曲",FileDialog.LOAD);
fd.setVisible(true);
path = fd.getDirectory()+fd.getFile();
boolean flag = false;
if (path.equals("")) {
return;
} else if (path.equals("nullnull")) {
return;
}
//如果JList中存在要添加的路径,则不继续在JList添加
for(int i=0;i<list.size();i++){
if(list.get(i).toString().equals(path)){
flag = true;
break;
}
}
//反之,则添加
if(flag==false){
list.add(path);
jl.setListData(list);
}
String lrcName = getLrcName(path);
try {
getLrcContxt(lrcName,jta);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println(path);
tempPlayer = PlayMusic.playMusic(path);
}
});
jmi_stop.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
PlayMusic.stopMusic(tempPlayer);
}
});
}
public static void main(String args[]){
PlayerFrame pf = new PlayerFrame();
}
private JScrollPane jspl;
private JScrollPane jspr;
private JList jl;
private JTextArea jta;
private JMenuBar jmb;
private JMenu jm;
private JMenuItem jmi_open;
private JMenuItem jmi_stop;
private static String path;
private static Vector list = new Vector();
private static Player tempPlayer = null;
private static Boolean flag_contxt = true;
}