代码(一个简单的GUI,通过菜单打开文件并播放,要停止播放直接关闭窗口即可。) SjzLinux联盟
/** SjzLinux联盟
* @kissplayer.java SjzLinux联盟
* SjzLinux联盟
* SjzLinux联盟
* @author SjzLinux联盟
* @version 1.00 2007/10/13 SjzLinux联盟
*/ SjzLinux联盟
import java.awt.*; SjzLinux联盟
import javax.swing.*; SjzLinux联盟
import java.awt.event.*; SjzLinux联盟
import javax.swing.event.*; SjzLinux联盟
import java.io.*; SjzLinux联盟
import javax.media.*; SjzLinux联盟
import java.io.File; SjzLinux联盟
import java.io.IOException; SjzLinux联盟
import java.net.URL; SjzLinux联盟
import java.net.MalformedURLException; SjzLinux联盟
class KissFrame extends JFrame{ SjzLinux联盟
KissFrame() { SjzLinux联盟
super("Simple Video Player"); SjzLinux联盟
setSize(400, 300); SjzLinux联盟
setDefaultCloseOperation(EXIT_ON_CLOSE); SjzLinux联盟
JMenuBar jmb = new JMenuBar(); SjzLinux联盟
JMenu jMenu1 = new JMenu("文件"); SjzLinux联盟
JMenu jMenu2 = new JMenu("控制"); SjzLinux联盟
JMenuItem jMenuItem1_1 = new JMenuItem("打开"); SjzLinux联盟
JMenuItem jMenuItem1_2 = new JMenuItem("退出"); SjzLinux联盟
jmb.add(jMenu1); SjzLinux联盟
jmb.add(jMenu2); SjzLinux联盟
jMenu1.add(jMenuItem1_1); SjzLinux联盟
jMenu1.add(jMenuItem1_2); SjzLinux联盟
// Add action HERE SjzLinux联盟
MenuItemListener listener = new MenuItemListener(); SjzLinux联盟
jMenuItem1_1.addActionListener(listener); SjzLinux联盟
setJMenuBar(jmb); SjzLinux联盟
Container contentPane = getContentPane(); SjzLinux联盟
} SjzLinux联盟
private class MenuItemListener implements ActionListener { SjzLinux联盟
public void actionPerformed(ActionEvent event) { SjzLinux联盟
// System.out.println("jMenuItem1_1"); SjzLinux联盟
JFileChooser chooser = new JFileChooser("."); // "."表示本目录 SjzLinux联盟
int result = chooser.showOpenDialog(null); SjzLinux联盟
if(result == chooser.APPROVE_OPTION) SjzLinux联盟
{ SjzLinux联盟
File selectedFile = chooser.getSelectedFile(); SjzLinux联盟
SimpleAudioPlayer.beginPlay(selectedFile); SjzLinux联盟
} SjzLinux联盟
else if(result == chooser.CANCEL_OPTION) SjzLinux联盟
{ SjzLinux联盟
// 用户取消了操作 SjzLinux联盟
} SjzLinux联盟
} SjzLinux联盟
} SjzLinux联盟
public static void main (String[] args) { SjzLinux联盟
KissFrame sf = new KissFrame(); SjzLinux联盟
sf.setVisible(true); SjzLinux联盟
} SjzLinux联盟
} SjzLinux联盟
class SimpleAudioPlayer { SjzLinux联盟
private Player audioPlayer = null; SjzLinux联盟
public SimpleAudioPlayer(URL url) throws IOException, SjzLinux联盟
NoPlayerException, SjzLinux联盟
CannotRealizeException { SjzLinux联盟
audioPlayer = Manager.createRealizedPlayer(url); SjzLinux联盟
} SjzLinux联盟
public SimpleAudioPlayer(File file) throws IOException, SjzLinux联盟
NoPlayerException, SjzLinux联盟
CannotRealizeException { SjzLinux联盟
this(file.toURL()); SjzLinux联盟
} SjzLinux联盟
public void play() { SjzLinux联盟
audioPlayer.start(); SjzLinux联盟
} SjzLinux联盟
public void stop() { SjzLinux联盟
audioPlayer.stop(); SjzLinux联盟
audioPlayer.close(); SjzLinux联盟
} SjzLinux联盟
public static void beginPlay(File audioFile) { SjzLinux联盟
try { SjzLinux联盟
SimpleAudioPlayer player = new SimpleAudioPlayer(audioFile); SjzLinux联盟
player.play(); SjzLinux联盟
// player.stop(); SjzLinux联盟
} catch(IOException e) { SjzLinux联盟
System.out.println(e.getMessage()); SjzLinux联盟
} catch(NoPlayerException ee) { SjzLinux联盟
System.out.println(ee.getMessage()); SjzLinux联盟
} catch(CannotRealizeException eee) { SjzLinux联盟
System.out.println(eee.getMessage()); SjzLinux联盟
} SjzLinux联盟
} SjzLinux联盟
} SjzLinux联盟