求助,关于音乐播放器的程序
小弟自己编了个简易的mp3播放器,, 这个程序一运行,就能弹出个界面,然后手动载入音乐文件就能播放音乐了. 现在的问题是,我希望我一运行这个程序,不需要通过手动载入音乐文件,而是自动载入某首音乐,, 然后就可以播放音乐了. 我的意思就是我一运行这个程序,就会有音乐自动响起来,请问各位前辈,应该怎样修改这个程序呢.程序如下:import java.awt.BorderLayout;
import java.awt.CheckboxMenuItem;
import java.awt.Color;
import java.
import java.awt.Container;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.ControllerClosedEvent;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.EndOfMediaEvent;
import javax.media.GainControl;
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;
class MediaPlayer
extends JFrame
implements ActionListener, ControllerListener, ItemListener {
Player player;
Component vc, cc, gcc;
GainControl gc;
boolean first = true, loop = false;
String currentDirectory;
MediaPlayer(String title) {
super(title);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
public void windowClosed(WindowEvent e) {
if (player != null)
player.close();
System.exit(0);
}
});
setupMenu();
setSize(400, 400);
setVisible(true);
}
public void setupMenu() {
Menu f = new Menu("file");
MenuItem mi = new MenuItem("open");
mi.addActionListener(this);
f.add(mi);
f.addSeparator();
CheckboxMenuItem cbmi = new CheckboxMenuItem("cycle", false);
cbmi.addItemListener(this);
f.add(cbmi);
f.addSeparator();
mi = new MenuItem("exit");
mi.addActionListener(this);
f.add(mi);
Menu l = new Menu("playlist");
Menu c = new Menu("control menu");
MenuItem move = new MenuItem("play");
move.addActionListener(this);
c.add(move);
c.addSeparator();
MenuItem pause = new MenuItem("pause");
pause.addActionListener(this);
c.add(pause);
c.addSeparator();
MenuItem stop = new MenuItem("stop");
stop.addActionListener(this);
c.add(stop);
c.addSeparator();
MenuBar mb = new MenuBar();
mb.add(f);
mb.add(c);
mb.add(l);
setMenuBar(mb);
}
public void actionPerformed(ActionEvent e) {
String cufile , selcectfile;
if (e.getActionCommand().equals("exit")) {
dispose();
return;
}
if (e.getActionCommand().equals("play")) {
if (player != null) {
player.start();
}
return;
}
if (e.getActionCommand().equals("pause")) {
if (player != null) {
player.stop();
}
return;
}
if (e.getActionCommand().equals("stop")) {
if (player != null) {
player.stop();
player.setMediaTime(new Time(0));
}
return;
}
if (e.getActionCommand().equals("open")) {
FileDialog fd = new FileDialog(this, "open mediafile", FileDialog.LOAD);
fd.setDirectory(currentDirectory);
fd.show();
if (fd.getFile() == null)
return;
selcectfile = fd.getFile();
currentDirectory = fd.getDirectory();
cufile = currentDirectory + selcectfile;
MenuItem mi = new MenuItem(selcectfile);
//mi.setActionCommand(cufile);
MenuBar mb = getMenuBar();
Menu m = mb.getMenu(2);
mi.addActionListener(this);
m.add(mi);
} else {
cufile = e.getActionCommand();
selcectfile = cufile;
}
if (player != null)
player.close();
try {
player = Manager.createPlayer(new MediaLocator("file:" + cufile));
} catch ( e2) {
System.out.println(e2);
return;
} catch (NoPlayerException e2) {
System.out.println("can not find player.");
return;
}
if (player == null) {
System.out.println("can not set up player.");
return;
}
first = false;
setTitle(selcectfile);
player.addControllerListener(this);
player.prefetch();
}
public void controllerUpdate(ControllerEvent e) {
Container tainer = getContentPane();
if (e instanceof ControllerClosedEvent) {
if (vc != null) {
remove(vc);
vc = null;
}
if (cc != null) {
remove(cc);
cc = null;
}
return;
}
if (e instanceof EndOfMediaEvent) {
player.setMediaTime(new Time(0));
if (loop) {
player.start();
}
return;
}
if (e instanceof PrefetchCompleteEvent) {
player.start();
return;
}
if (e instanceof RealizeCompleteEvent) {
vc = player.getVisualComponent();
if (vc != null)
tainer.add(vc, BorderLayout.CENTER);
cc = player.getControlPanelComponent();
cc.setBackground(Color.BLUE);
if (cc != null)
tainer.add(cc, BorderLayout.SOUTH);
/*gc = player.getGainControl();
gcc = gc.getControlComponent();
if (gcc != null)
tainer.add(gcc, BorderLayout.NORTH);*/
if (vc != null) {
pack();
return;
} else {
setSize(300, 75);
setVisible(true);
return;
}
}
}
public void itemStateChanged(ItemEvent e) {
loop = !loop;
}
public void update(Graphics g) {
paint(g);
}
public static void main(String[] args) {
new MediaPlayer("Simpler mediaplayer");
}
public void paint(Graphics g) {
super.paint(g);
//interface
if (first) {
int w = getSize().width;
int h = getSize().height;
g.setColor(Color.magenta);
g.fillRect(0, 0, w, h);
Font f = new Font("DialogInput", Font.BOLD, 18);
g.setFont(f);
FontMetrics fm = g.getFontMetrics();
int swidth = fm.stringWidth(" HU's MP3 PLAYER ");
g.setColor(Color.white);
g.drawString(
" HU's MP3 PLAYER ",
(w - swidth) / 2,
(h + getInsets().top) / 2);
}
}
}