JMF这个多媒体开发框架太牛了,简单的几句代码就能实现一个视频播放器的开发,厉害,就是支持的格式少了一些,没关系,这个视频播放器可以播放mpg,avi,fvl等等,想播放其他的请开发自己的插件,下面将代码贴上去。
import java.awt.Button;
import java.awt.Choice;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URL;
import javax.media.Manager;
import javax.media.Player;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
public class MP3 {
static final String PATH = "D:/Project/NewDemo//Cityscape/"; // 音樂文件的位置
String url;// 要播放的地址
Player player;
public MP3() {
}
public void setUrl(String url) {
this.url = url;
}
public void play() {
try {
player = Manager.createPlayer(new URL("file:/" + url));
player.realize();
player.start();
} catch (Exception e) {
System.out.println("Cannot find the" + url);
}
}
public void stop() {
player.stop();
player.deallocate();
}
public static void main(String[] args) {
JFrame frame = new JFrame();
Panel panel = new Panel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
Choice choice = new Choice();
File file = new File(PATH);
String[] files = file.list();
for (String string : files) {
if (string.indexOf(".wav") != -1 || string.indexOf(".mp3") != -1)
choice.add(string);
}
panel.add(choice);
Button play = new Button("Play");
Button stop = new Button("Stop");
MP3 mp3 = new MP3();
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == play) {
String url = PATH + choice.getSelectedItem();
mp3.setUrl(url);
mp3.play();
} else if (e.getSource() == stop) {
mp3.stop();
}
}
};
play.addActionListener(listener);
stop.addActionListener(listener);
panel.add(play);
panel.add(stop);
frame.add(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

本文链接:https://it72.com:4443/11744.htm