📄 musicplayer.java
字号:
/*
* MusicPlayer.java
*
* Created on 2008年3月28日, 下午10:16
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.gjun.huxiaoming;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Hashtable;
import javax.media.Control;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.EndOfMediaEvent;
import javax.media.Manager;
import javax.media.Player;
import javax.media.Time.*;
import javax.media.RealizeCompleteEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.media.bean.playerbean.MediaPlayer;
/**
*
* @author Administrator
*/
public class MusicPlayer extends JFrame implements ControllerListener{
// public ReadSong readSong;
private Player player = null;
private Component center, south;
private File file;
private Hashtable table=new Hashtable();
private MediaPlayer mdp=new MediaPlayer();
private Hashtable tablePath=new Hashtable();
private ArrayList songArray=new ArrayList();
List songList=new List();
//create panel p1
private JPanel p1=new JPanel();
private ImageIcon image1=new ImageIcon("名字.jpg");
private JLabel jlabel1=new JLabel("welcome to my player");
private JLabel jlabel2=new JLabel();
private JPanel p5=new JPanel();
//create panel p2 and the component that will add to p2
private JPanel p2=new JPanel();
private JPanel jpanel1=new JPanel();
private ImageIcon image2=new ImageIcon("next.jpg");
private ImageIcon image3=new ImageIcon("back.jpg");
private ImageIcon image4=new ImageIcon("stop3.png");
JButton jb1=new JButton("next");
JButton jb2=new JButton("back");
JButton jb3=new JButton("stop");
JButton jb4=new JButton("loop");
private JPanel jpanel2=new JPanel();
JButton jb5=new JButton("open");
JButton jb6=new JButton("remove");
JButton jb7=new JButton("skins");
JPopupMenu popMenu=new JPopupMenu();
private JLabel jlabel3=new JLabel("play list");
private JPanel p4=new JPanel();
private JPanel jlabel4=new JPanel();
//create panel p3 and the component that will add to p3
private JPanel p3=new JPanel();
private List list=new List();
private JPanel p6=new JPanel();
JFrame frame=new JFrame("歌词秀");
private boolean isStop=false;
// List songList=new List();
/** Creates a new instance of MusicPlayer */
public MusicPlayer()
{
//窗口的设置
super("My Player");
setSize(250,350);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(1);
}
});
Container pane=getContentPane();
GridLayout layout1=new GridLayout(3,1);
pane.setLayout(layout1);
p1.setLayout(layout1);
p1.add(jlabel1);
p1.add(jlabel2);
p5.setBackground(new Color(160,32,200).brighter());
p1.add(p5);
p1.setBackground(new Color(160,32,200).brighter());
pane.add(p1);
setVisible(true);
GridLayout layout2=new GridLayout(1,4,5,5);
jpanel1.setLayout(layout2);
jpanel1.add(jb1);
jpanel1.add(jb2);
jpanel1.add(jb3);
jpanel1.add(jb4);
jb1.setBackground(new Color(160,32,200).brighter());
jb2.setBackground(new Color(160,32,200).brighter());
jb3.setBackground(new Color(160,32,200).brighter());
jb4.setBackground(new Color(160,32,200).brighter());
jpanel1.setBackground(new Color(160,32,200).brighter());
p4.setBackground(new Color(160,32,200).brighter());
// p4.setLayout(new GridLayout(1,2));
p4.add(jpanel1);
// p4.add(jlabel4);
GridLayout layout3=new GridLayout(1,3,5,4);
jpanel2.setLayout(layout3);
jpanel2.add(jb5);
jb5.setBackground(new Color(160,32,200).brighter());
jb6.setBackground(new Color(160,32,200).brighter());
jb7.setBackground(new Color(160,32,200).brighter());
jpanel2.add(jb6);
jpanel2.add(jb7);
jpanel2.setBackground(new Color(160,32,200).brighter());
p2.setLayout(layout1);
p2.setBackground(new Color(160,32,200).brighter());
p2.add(p4);
p2.add(jlabel3);
p2.add(jpanel2);
pane.add(p2);
pane.add(list);
list.setBackground(new Color(160,32,200).brighter());
JMenuItem item1=new JMenuItem("Aqua");
JMenuItem item2=new JMenuItem("Blue");
JMenuItem item3=new JMenuItem("Cyber Green");
JMenuItem item4=new JMenuItem("Violet");
popMenu.add(item1);
popMenu.add(item2);
popMenu.add(item3);
popMenu.add(item4);
item1.addActionListener(new itemAction());
item2.addActionListener(new item2Action());
item3.addActionListener(new item3Action());
item4.addActionListener(new item4Action());
// jb7.add(popMenu);
setVisible(true);
//打开文件的事件,显示系统的文件,选择要播放的歌曲,
jb5.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JFileChooser fc = new JFileChooser(file); //显示文件
if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) //选择确认后返回
{
file = fc.getSelectedFile();//file是选择确认后的文件
String name=file.getName();
list.add( file.getName());
System.out.println("the path is"+file.getPath());
try{
table.put(name,file.toURL()); //存储歌曲的URL
tablePath.put(name,file.getAbsolutePath());
}
catch(Exception ev)
{
}
}
}
});
list.addActionListener(new ActionListener() //选择播放歌曲,双击列表事件
{
public void actionPerformed(ActionEvent evt)
{
List lt1=(List)evt.getSource();
jlabel2.setText("正在播放:"+lt1.getSelectedItem());
String song=lt1.getSelectedItem();
URL url=(URL)table.get(lt1.getSelectedItem());
System.out.println("the url is"+url);
playerStart(song);
String filePath=(String)tablePath.get(lt1.getSelectedItem());
}
}) ;
jb1.addActionListener(new ActionListener() //后一首歌
{
public void actionPerformed(ActionEvent e)
{
isStop=false;
int i=list.getSelectedIndex();
String list2=list.getItem(i+1); //获得后一首歌
list.select(i+1); //改变列表中选择的歌曲
playerStart(list2); //播放歌曲
}
}
);
jb2.addActionListener(new ActionListener() //前一首歌
{
public void actionPerformed(ActionEvent e)
{
isStop=false;
int i=list.getSelectedIndex();
String list2=list.getItem(i-1);
list.select(i-1);
URL url=(URL)table.get(list2); //取出前一首的URL
playerStart(list2); //播放前一首歌
}
}
);
jb3.addActionListener(new ActionListener() //停止播放
{
public void actionPerformed(ActionEvent e)
{
isStop=false;
if (player != null) //判断是否在播放
{
player.stop(); //如果是就停止
player.close(); //关闭
}
}
}
);
jb4.addActionListener(new ActionListener() //循环播放
{
public void actionPerformed(ActionEvent e)
{
if(mdp!=null)
{
mdp.setPlaybackLoop(true); //循环播放歌曲
}
}
});
jb6.addActionListener(new ActionListener() //删除播放歌曲
{
public void actionPerformed(ActionEvent e)
{
int i=list.getSelectedIndex();
String st=list.getItem(i);
list.remove(i); //从列表中删除
table.remove(st); //从URL表中删除
}
});
jb7.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
popMenu.show(jb7,jb6.getX(),jb6.getY());
//
}
});
frame.setSize(150,350);
// frame.setForeground(new Color(180,255,204));
// frame.add(songList);
songList.setBackground(new Color(160,32,200).brighter());
frame.setBackground(new Color(160,32,200).brighter());
}
public static void main(String arg[])
{
new MusicPlayer();
}
public void controllerUpdate(ControllerEvent ce) {
if (ce instanceof RealizeCompleteEvent) {
if(south != null)
p5.remove(south);
if ((south = player.getControlPanelComponent())!= null) //加上播放的控件
// south.setBackground(Color.ORANGE);
south.setSize(245,50);
p5.setLayout(new GridLayout(1,1)) ;
p5.setForeground(new Color(0,25,24));
p5.add(south);
validate();
}
if(ce instanceof EndOfMediaEvent) //判断是否是播放到结尾了,接着播放下一首歌
{
try {
Thread.sleep(1 * 1000);
}
catch (InterruptedException ex) {
}
if(!mdp.getPlaybackLoop())
{
frame.remove(songList);
int i=list.getSelectedIndex();
try{
String list2=list.getItem(i+1); //获得后一首歌
list.select(i+1); //改变列表中选择的歌曲
URL url=(URL)table.get(list2); //取出要播放的歌曲的URL
playerStart(list2);
//播放歌曲
// player.getMediaTime()
} catch(ArrayIndexOutOfBoundsException e)
{
player.stop();
player.close();
}
}
}
}
public void playerStart(String songName)
{
URL url=(URL)table.get(songName);
String filePath=(String)tablePath.get(songName);
if (player != null) //判断是否在播放
{
player.stop(); //如果是就停止
player.close(); //关闭
}
try {
player = Manager.createPlayer(url);//获得player的对象
mdp.setPlayer(player);
mdp.setPlaybackLoop(false);
player.addControllerListener(MusicPlayer.this); //加入监听器
System.out.println();
mdp.start();
frame.setVisible(false);
ReadSong readSong=new ReadSong(filePath);
songList.removeAll();
songList=readSong.getList();
/* for(int k=0;k<readSong.getList().getRows();k++)
{
songList.add(readSong.getList().getItem(k));
}*/
songArray=readSong.getArrayTime();
Color color=jpanel1.getBackground();
//isStop=true;
setSongList set=new setSongList();
Thread t1=new Thread(set);
t1.start();
frame.add(songList);
songList.setBackground(color);
frame.setBackground(color);
frame.setVisible(true);
} catch (Exception ex) {
ex.printStackTrace();
}
}
class setSongList implements Runnable
{
public void run()
{
for(int t=0;t<songArray.size()-1;t++)
{
songList.select(t+5);
Long d=((Long)songArray.get(t+1))-((Long)songArray.get(t));
long l=d.longValue();
try {
Thread.sleep(l);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
class itemAction implements ActionListener
{
public void actionPerformed(ActionEvent ace)
{
//if(ace.getSource())
JMenuItem mi=(JMenuItem)ace.getSource();
Color color;
color=new Color(127,255,212) ;
setColor(color);
}
}
class item2Action implements ActionListener
{
public void actionPerformed(ActionEvent ace)
{
JMenuItem mi=(JMenuItem)ace.getSource();
Color color=new Color(0,0,255).brighter();
setColor(color);
}
}
class item3Action implements ActionListener
{
public void actionPerformed(ActionEvent ace)
{
JMenuItem mi=(JMenuItem)ace.getSource();
Color color=Color.green.darker().brighter();
setColor(color);
}
}
class item4Action implements ActionListener
{
public void actionPerformed(ActionEvent ace)
{
JMenuItem mi=(JMenuItem)ace.getSource();
Color color=new Color(238,130,250);
setColor(color);
}
}
public void setColor(Color color)
{ jpanel1.setBackground(color);
p5.setBackground(color);
p1.setBackground(color);
jb1.setBackground(color);
jb2.setBackground(color);
jb3.setBackground(color);
jb4.setBackground(color);
jb5.setBackground(color);
jb6.setBackground(color);
jb7.setBackground(color);
jpanel2.setBackground(color);
p2.setBackground(color);
list.setBackground(color);
p4.setBackground(color);
frame.setForeground(color);
songList.setBackground(color);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -