代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
使用JMF编写媒体播放器
作者:
Hsingmin
/ 发布于
2015/9/14
/
682
/** * @Description : A media palyer for local file and web file. * @Author : Alfred Lee * @Date : Aug. 29th, 2015 * @Email : alfred_bit@sina.cn */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.media.*; import java.net.URL; import java.net.MalformedURLException; public class RePlayer{ public static void main(String[] args){ EventQueue.invokeLater(new Runnable(){ public void run(){ new MultiPlayer("Open Source"); System.out.println("Create new player."); } }); } } class MultiPlayer extends JFrame implements ControllerListener, ItemListener{ public MultiPlayer(String title){ super(title); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent event){ dispose(); } public void windowClosed(WindowEvent event){ if(player != null) player.close(); System.exit(0); } }); setupMenu(); setSize(FRAME_WIDTH,FRAME_HEIGHT); setVisible(true); } public void setupMenu(){ //File menu Menu fileMenu = new Menu("File"); MenuItem openItem = new MenuItem("Open"); openItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ FileDialog fd = new FileDialog(new Frame(),"Open Media File",FileDialog.LOAD); fd.setDirectory(currentDirectory); fd.show(); if(fd.getFile() == null) return; selectfile = fd.getFile(); currentDirectory = fd.getDirectory(); currentfile = currentDirectory + selectfile; url = "file:" + currentfile; //add new item into list menu when openning a local media file MenuItem menuItem = new MenuItem(selectfile); menuItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ url = "file:" + selectfile; System.out.println("Open file : " + url); setPlayer(url); } }); MenuBar menuBar = getMenuBar(); Menu listMenu = menuBar.getMenu(3); listMenu.add(menuItem); setPlayer(url); } }); fileMenu.add(openItem); fileMenu.addSeparator(); //loop play mode CheckboxMenuItem loopItem = new CheckboxMenuItem("Loop",false); loopItem.addItemListener(this); fileMenu.add(loopItem); fileMenu.addSeparator(); //quit player MenuItem quitItem = new MenuItem("Quit"); quitItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ dispose(); return; } }); fileMenu.add(quitItem); //URL input menu Menu urlMenu = new Menu("URL"); MenuItem urlItem = new MenuItem("Input"); urlItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ if(dialog == null) dialog = new InputDialog(MultiPlayer.this); url = dialog.getUrl(); System.out.println("Open url : " + url); setPlayer(url); //add a new item into list menu when openning a url MenuItem menuItem = new MenuItem(url); menuItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ setPlayer(url); } }); MenuBar menuBar = getMenuBar(); Menu listMenu = menuBar.getMenu(3); listMenu.add(menuItem); } }); urlMenu.add(urlItem); //Player control menu Menu controlMenu = new Menu("Control"); MenuItem playItem = new MenuItem("Play"); playItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ if(player != null) player.start(); return; } }); controlMenu.add(playItem); controlMenu.addSeparator(); MenuItem pauseItem = new MenuItem("Pause"); pauseItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ if(player != null) player.stop(); return; } }); controlMenu.add(pauseItem); controlMenu.addSeparator(); MenuItem stopItem = new MenuItem("Stop"); stopItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ if(player != null){ player.stop(); player.setMediaTime(new Time(0)); } return; } }); controlMenu.add(stopItem); //Player list menu Menu listMenu = new Menu("List"); MenuBar menuBar = new MenuBar(); menuBar.add(fileMenu); menuBar.add(urlMenu); menuBar.add(controlMenu); menuBar.add(listMenu); setMenuBar(menuBar); } public void setPlayer(String url){ if(player != null) player.close(); try{ player = Manager.createPlayer(new MediaLocator(url)); } catch(java.io.IOException e2){ System.out.println(e2); return; } catch(NoPlayerException e2){ System.out.println("Cannot find the player!"); return; } if(player == null){ System.out.println("Cannot create the player!"); return; } setTitle(url); player.addControllerListener(MultiPlayer.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); } } } public void itemStateChanged(ItemEvent e){ loop = !loop; } public void update(Graphics g){ paint(g); } public void paint(Graphics g){ super.paint(g); if(first){ int width = getSize().width; int height = getSize().height; g.setColor(Color.magenta); g.fillRect(0,0,width,height); Font font = new Font("DialogInput",Font.BOLD,18); g.setFont(font); FontMetrics fontMetrics = g.getFontMetrics(); int swidth = fontMetrics.stringWidth("Repro Player"); g.setColor(Color.white); g.drawString("Repro Player",(width - swidth)/2,(height + getInsets().top)/2); } } private Player player; private InputDialog dialog; private String url; private String selectfile; private String currentfile; private Component vc, cc, gcc; private GainControl gc; private boolean first = true; private boolean loop = false; private String currentDirectory; private static final int FRAME_WIDTH = 450; private static final int FRAME_HEIGHT = 450; } class InputDialog extends JDialog{ public InputDialog(JFrame owner){ super(owner,"Input Dialog",true); final JTextField inputField = new JTextField(); add(inputField,BorderLayout.NORTH); JButton ok = new JButton("OK"); ok.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ urlValue = inputField.getText().toString(); setVisible(false); } }); JPanel panel = new JPanel(); panel.add(ok); add(panel,BorderLayout.SOUTH); setSize(DIALOG_WIDTH,DIALOG_HEIGHT); setVisible(true); } public String getUrl(){ return urlValue; } private JTextField inputField; private String urlValue; private static final int DIALOG_WIDTH = 250; private static final int DIALOG_HEIGHT = 150; }
试试其它关键字
媒体播放器
JMF
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Hsingmin
贡献的其它代码
(
2
)
.
校正身份证
.
使用JMF编写媒体播放器
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3