package mp3thinkplayer;
import java.awt.FileDialog;
import javax.media.ControllerEvent;
import javax.media.EndOfMediaEvent;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoPlayerException;
import javax.media.StartEvent;
import javax.media.Time;
/**
*
* @author Administrator
*/
public class MainJFrame extends javax.swing.JFrame implements javax.media.ControllerListener{
javax.media.Player player; //播放器组件
FileDialog fd;//对话框
/** Creates new form MainJFrame */
public MainJFrame() {
initComponents();//记得先初始化,日
this.playMenuItem.setEnabled(false);
this.pauseMenuItem.setEnabled(false);
this.stopMenuItem.setEnabled(false);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
fileLabel = new javax.swing.JLabel();
jMenuBar1 = new javax.swing.JMenuBar();
fileMenu = new javax.swing.JMenu();
openMenuItem = new javax.swing.JMenuItem();
playMenu = new javax.swing.JMenu();
playMenuItem = new javax.swing.JMenuItem();
pauseMenuItem = new javax.swing.JMenuItem();
stopMenuItem = new javax.swing.JMenuItem();
loopMenuItem = new javax.swing.JCheckBoxMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("我的Mp3播放器");
setBackground(new java.awt.Color(255, 51, 0));
setBounds(new java.awt.Rectangle(50, 50, 200, 20));
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
setFont(new java.awt.Font("隶书", 0, 14)); // NOI18N
setForeground(java.awt.Color.pink);
fileLabel.setText("播放文件名");
jMenuBar1.setBackground(new java.awt.Color(0, 204, 204));
jMenuBar1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
jMenuBar1.setToolTipText("播放菜单栏");
jMenuBar1.setFont(new java.awt.Font("宋体-方正超大字符集", 2, 18)); // NOI18N
fileMenu.setIcon(new javax.swing.ImageIcon("D:\\QQ2008\\Face2\\121.gif")); // NOI18N
fileMenu.setText("文件");
openMenuItem.setText("打开");
openMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
openMenuItemMouseClicked(evt);
}
public void mousePressed(java.awt.event.MouseEvent evt) {
openMenuItemMousePressed(evt);
}
});
openMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
openMenuItemActionPerformed(evt);
}
});
fileMenu.add(openMenuItem);
jMenuBar1.add(fileMenu);
playMenu.setIcon(new javax.swing.ImageIcon("D:\\QQ2008\\Face2\\114.gif")); // NOI18N
playMenu.setText("工具");
playMenuItem.setText("播放");
playMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
playMenuItemMousePressed(evt);
}
});
playMenu.add(playMenuItem);
pauseMenuItem.setText("暂停");
pauseMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
pauseMenuItemMousePressed(evt);
}
});
playMenu.add(pauseMenuItem);
stopMenuItem.setText("停止");
stopMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
stopMenuItemMousePressed(evt);
}
});
playMenu.add(stopMenuItem);
loopMenuItem.setSelected(true);
loopMenuItem.setText("循环");
playMenu.add(loopMenuItem);
jMenuBar1.add(playMenu);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(112, 112, 112)
.addComponent(fileLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(212, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(fileLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(98, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuItemActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_openMenuItemActionPerformed
private void openMenuItemMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_openMenuItemMouseClicked
// TODO add your handling code here:
}//GEN-LAST:event_openMenuItemMouseClicked
private void openMenuItemMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_openMenuItemMousePressed
fd = new FileDialog(this, "打开媒体文件",FileDialog.LOAD);
fd.show();
/*
* 如果有选择文件,则进行播放
*/
if(fd.getDirectory()!=null)
{
//先停止当前正在播放的文件
if(this.player!=null)
{
this.player.stop();
if(this.player.getControlPanelComponent()!=null)
{
//移除当前的播放器组件
this.remove(this.player.getControlPanelComponent());
}
}
try
{
//导入新文件
player = Manager.createPlayer(new MediaLocator
("file:" + fd.getDirectory() + fd.getFile()));
//显示文件名
this.fileLabel.setText(fd.getFile());
}
catch (java.io.IOException e2)
{
System.out.println(e2.getMessage());
}
catch (NoPlayerException e2)
{
System.out.println("不能找到播放器.");
}
if(player!=null)
{
this.player.start();//播放音乐
this.player.prefetch();
if(player!=null)
{
try
{
player.addControllerListener(this);//添加事件监听者
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
}
}//GEN-LAST:event_openMenuItemMousePressed
private void pauseMenuItemMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pauseMenuItemMousePressed
if(this.pauseMenuItem.isEnabled())
{
if(this.player!=null)
{
this.player.stop();
}
}
}//GEN-LAST:event_pauseMenuItemMousePressed
private void playMenuItemMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_playMenuItemMousePressed
if(this.playMenuItem.isEnabled())
{
if(this.player!=null)
{
this.player.start();
}
}
}//GEN-LAST:event_playMenuItemMousePressed
private void stopMenuItemMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_stopMenuItemMousePressed
if(this.stopMenuItem