求 简单的JAVA 音乐播放器 代码
只要求在一个JPanel中,有一个 “播放/暂停” 按钮、一个 “下一首” 按钮 和调解音量的条(你懂得),
播放一个路径(假设D:\music\)文件夹里的mp3格式文件,
当然最好可以在播放按钮和调节音量下面设置一个列表,有音乐名称,点一首音乐,然后点播放就放这首歌了。
谢谢哈
[解决办法]
音乐播放器\lib\basicplayer2.3.jar
..........\...\jl0.4.jar
..........\...\mp3spi1.9.1.jar
..........\...\tritonus_share.jar
..........\musicplayer\FileInput.class
..........\...........\MusicPlayer$1.class
..........\...........\MusicPlayer$2.class
..........\...........\MusicPlayer$3.class
..........\...........\MusicPlayer$4.class
..........\...........\MusicPlayer$5.class
..........\...........\MusicPlayer$6.class
..........\...........\MusicPlayer$7.class
..........\...........\MusicPlayer$8.class
..........\...........\MusicPlayer.class
..........\...........\MyFileFilter.class
..........\...........\Play.class
..........\...........\States.class
..........\rum.bat
..........\多媒体论文.doc
..........\源代码\musicplayer\FileInput.java
..........\......\...........\MusicPlayer.java
..........\......\...........\MyFileFilter.java
..........\......\...........\Play.java
..........\......\...........\States.java
..........\运行说明.txt
..........\项目说明.txt
..........\源代码\musicplayer
..........\lib
..........\musicplayer
..........\源代码
[解决办法]
package com.test.media;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import javax.media.CannotRealizeException;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.EndOfMediaEvent;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.PrefetchCompleteEvent;
import javax.media.RealizeCompleteEvent;
import javax.media.Time;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class TestPlayer extends JFrame implements ActionListener,ControllerListener{
/**
*
*/
private static final long serialVersionUID = 1L;
private Player player;//播放器
private Component visual;//播放器组建
private Component control;//控制组建
private JMenu fileMenu;//文件菜单
private JMenuItem open;//打开文件菜单项
private JMenuItem exit;//退出菜单项
private JMenuBar menuBar;//菜单栏
public TestPlayer()
{
fileMenu=new JMenu("文件");
open=new JMenuItem("打开");
open.addActionListener(this);
exit=new JMenuItem("退出");
exit.addActionListener(this);
fileMenu.add(open);
fileMenu.addSeparator();
fileMenu.add(exit);
menuBar=new JMenuBar();
menuBar.add(fileMenu);
this.setJMenuBar(menuBar);
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
dispose();
System.exit(0);
}});
}
public void controllerUpdate(ControllerEvent ce) {
// TODO Auto-generated method stub
if(ce instanceof RealizeCompleteEvent)
{
player.prefetch();
}
else if(ce instanceof PrefetchCompleteEvent)
{
if((visual=player.getVisualComponent())!=null)
{
add(visual,BorderLayout.NORTH);
}
if((control=player.getControlPanelComponent())!=null)
{
add(control,BorderLayout.SOUTH);
}
player.start();
this.pack();
}
else if(ce instanceof EndOfMediaEvent)
{
player.setMediaTime(new Time(0));
player.start();
}
}
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
Component source=(Component) event.getSource();
if(source==open)
{
FileDialog fileDialog=new FileDialog(this,"打开文件",FileDialog.LOAD);
fileDialog.setFile("*.mp3");
fileDialog.setVisible(true);
if(fileDialog.getFile()==null)
{
return;
}
try {
player=Manager.createPlayer(new MediaLocator("file:///"+fileDialog.getDirectory().replace("\", "/")+fileDialog.getFile()));
player.realize();
player.addControllerListener(this);
} catch (NoPlayerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if(source==exit)
{
dispose();
System.exit(0);
}
}
public static void main(String[] args)
{
new TestPlayer();
}
}