📄 fmplayer.java
字号:
import javax.media.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class FMPlayer extends Frame implements ControllerListener{
Player player; //JMF中的Player类
Component visualcmp, controlcmp; //视觉部件和控制部件
Panel pane;
TextField textfield;
Button button;
String string;
Label label;
FMPlayer (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);
}
});
//交互界面的
pane = new Panel();
string = new String();
label = new Label("要播放文件的URL:");
textfield = new TextField("http://localhost:8080/try.mpg",30);
button = new Button("确定");
button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button_actionPerformed(e);
}
});
add(pane,BorderLayout.NORTH);
pane.add(label);
pane.add(textfield);
pane.add(button);
setSize (450, 80);
setVisible (true);
}
public void button_actionPerformed (ActionEvent e) {
string = textfield.getText();
if(string.compareTo("") != 0){
if (player != null) player.close ();
try {
player = Manager.createPlayer (new MediaLocator (string));
}
catch (java.io.IOException e2) {
System.out.println (e2);
return;
}
catch (NoPlayerException e2) {
System.out.println ("不能创建播放器");
return;
}
}
if (player == null) {
System.out.println ("創建播放器失败.");
return;
}
player.addControllerListener (this); //监听控制事件
player.prefetch (); //预先提取媒体内容
}
public void controllerUpdate (ControllerEvent e) { //监听控制器
if (e instanceof ControllerClosedEvent) { //关闭控制器,移除部件
if (visualcmp != null) {
remove (visualcmp);
visualcmp = null;
}
if (controlcmp != null) {
remove (controlcmp);
controlcmp = null;
}
return;
}
if (e instanceof PrefetchCompleteEvent) { //预取媒体内容结束,开始播放
player.start ();
return;
}
if (e instanceof RealizeCompleteEvent) { //实例化结束
visualcmp = player.getVisualComponent ();
if (visualcmp != null) add (visualcmp,BorderLayout.CENTER);
controlcmp = player.getControlPanelComponent ();
if (controlcmp != null) add (controlcmp,BorderLayout.SOUTH);
pack (); //整合
}
}
public void paint (Graphics g) {
super.paint (g);//调用父类的方法
}
public void update (Graphics g) {
paint (g);
}
public static void main (String [] args) { //主程序
new FMPlayer ("流媒体播放器");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -