重慶分公司,新征程啟航
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務(wù)器等服務(wù)
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務(wù)器等服務(wù)
import java.awt.BorderLayout;
成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供橋東網(wǎng)站建設(shè)、橋東做網(wǎng)站、橋東網(wǎng)站設(shè)計、橋東網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、橋東企業(yè)網(wǎng)站模板建站服務(wù),10余年橋東做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
import java.awt.Component;
import java.awt.FileDialog;
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.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.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class JMFMediaPlayer extends JFrame implements ActionListener,
ControllerListener, ItemListener {
// JMF的播放器
Player player;
// 播放器的視頻組件和控制組件
Component vedioComponent;
Component controlComponent;
// 標(biāo)示是否是第一次打開播放器
boolean first = true;
// 標(biāo)示是否需要循環(huán)
boolean loop = false;
// 文件當(dāng)前目錄
String currentDirectory;
// 構(gòu)造方法
public JMFMediaPlayer(String title) {
super(title);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
// 用戶點擊窗口系統(tǒng)菜單的關(guān)閉按鈕
// 調(diào)用dispose以執(zhí)行windowClosed
dispose();
}
public void windowClosed(WindowEvent e){
if (player != null){
// 關(guān)閉JMF播放器對象
player.close();
}
System.exit(0);
}
});
// 創(chuàng)建播放器的菜單
JMenu fileMenu = new JMenu("文件");
JMenuItem openMemuItem = new JMenuItem("打開");
openMemuItem.addActionListener(this);
fileMenu.add(openMemuItem);
// 添加一個分割條
fileMenu.addSeparator();
// 創(chuàng)建一個復(fù)選框菜單項
JCheckBoxMenuItem loopMenuItem = new JCheckBoxMenuItem("循環(huán)", false);
loopMenuItem.addItemListener(this);
fileMenu.add(loopMenuItem);
fileMenu.addSeparator();
JMenuItem exitMemuItem = new JMenuItem("退出");
exitMemuItem.addActionListener(this);
fileMenu.add(exitMemuItem);
JMenuBar menuBar = new JMenuBar();
menuBar.add(fileMenu);
this.setJMenuBar(menuBar);
this.setSize(200, 200);
try {
// 設(shè)置界面的外觀,為系統(tǒng)外觀
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception e) {
e.printStackTrace();
}
this.setVisible(true);
}
/**
* 實現(xiàn)了ActionListener接口,處理組件的活動事件
*/
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("退出")) {
// 調(diào)用dispose以便執(zhí)行windowClosed
dispose();
return;
}
FileDialog fileDialog = new FileDialog(this, "打開媒體文件", FileDialog.LOAD);
fileDialog.setDirectory(currentDirectory);
fileDialog.setVisible(true);
// 如果用戶放棄選擇文件,則返回
if (fileDialog.getFile() == null){
return;
}
currentDirectory = fileDialog.getDirectory();
if (player != null){
// 關(guān)閉已經(jīng)存在JMF播放器對象
player.close();
}
try {
// 創(chuàng)建一個打開選擇文件的播放器
player = Manager.createPlayer(new MediaLocator("file:"
+ fileDialog.getDirectory() + fileDialog.getFile()));
} catch (java.io.IOException e2) {
System.out.println(e2);
return;
} catch (NoPlayerException e2) {
System.out.println("不能找到播放器.");
return;
}
if (player == null) {
System.out.println("無法創(chuàng)建播放器.");
return;
}
first = false;
this.setTitle(fileDialog.getFile());
// 播放器的控制事件處理
player.addControllerListener(this);
// 預(yù)讀文件內(nèi)容
player.prefetch();
}
/**
* 實現(xiàn)ControllerListener接口的方法,處理播放器的控制事件
*/
public void controllerUpdate(ControllerEvent e) {
// 調(diào)用player.close()時ControllerClosedEvent事件出現(xiàn)。
// 如果存在視覺部件,則該部件應(yīng)該拆除(為一致起見,
// 我們對控制面板部件也執(zhí)行同樣的操作)
if (e instanceof ControllerClosedEvent) {
if (vedioComponent != null) {
this.getContentPane().remove(vedioComponent);
this.vedioComponent = null;
}
if (controlComponent != null) {
this.getContentPane().remove(controlComponent);
this.controlComponent = null;
}
return;
}
// 如果是媒體文件到達尾部事件
if (e instanceof EndOfMediaEvent) {
if (loop) {
// 如果允許循環(huán),則重新開始播放
player.setMediaTime(new Time(0));
player.start();
}
return;
}
// 如果是播放器預(yù)讀事件
if (e instanceof PrefetchCompleteEvent) {
// 啟動播放器
player.start();
return;
}
// 如果是文件打開完全事件,則顯示視頻組件和控制器組件
if (e instanceof RealizeCompleteEvent) {
vedioComponent = player.getVisualComponent();
if (vedioComponent != null){
this.getContentPane().add(vedioComponent);
}
controlComponent = player.getControlPanelComponent();
if (controlComponent != null){
this.getContentPane().add(controlComponent, BorderLayout.SOUTH);
}
this.pack();
}
}
// 處理“循環(huán)”復(fù)選框菜單項的點擊事件
public void itemStateChanged(ItemEvent e) {
loop = !loop;
}
public static void main(String[] args){
new JMFMediaPlayer("JMF媒體播放器");
}
}
試試吧,我這里運行正常
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java點虐 .*;
import javax.swing.*;
import javax.media.*;
// 視頻播放程序
public class VideoPlayDemo extends JFrame {
private Player player; // 播放器對象
private Component visualMedia; // 視頻顯示組件
private Component mediaControl; // 視頻播放控制組件
private Container container; // 主容器
private File mediaFile; //媒體文件
private URL fileURL; //媒體文件URL地址
public VideoPlayDemo() { // 構(gòu)造函數(shù)
super("視頻播放程序"); //調(diào)用父類構(gòu)造函數(shù)
container = getContentPane(); //得到窗口容器
JToolBar toobar = new JToolBar(); //實例化工具欄
JButton openFile = new JButton("打開媒體文件"); //實例化按鈕
toobar.add(openFile); //增加按鈕到工具欄
JButton openURL = new JButton("打開網(wǎng)絡(luò)地址");
toobar.add(openURL);
container.add(toobar, BorderLayout.NORTH); //設(shè)置工具欄
openFile.addActionListener(new ActionListener() { //打開文件按鈕事件處理
public void actionPerformed(ActionEvent event) {
JFileChooser fileChooser = new JFileChooser(); //實例化文件選擇器
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);//設(shè)置文件打開模式為僅打開文件
int result = fileChooser.showOpenDialog(VideoPlayDemo.this);//顯示對話框
if (result == JFileChooser.APPROVE_OPTION) { //得到用戶行為
mediaFile = fileChooser.getSelectedFile(); //得到選擇的文件
}
if (mediaFile != null) {
try {
fileURL = mediaFile.toURL(); //得到文件的URL地址
} catch (MalformedURLException ex) {
ex.printStackTrace(); //輸出錯誤信息
showMessage("打開錯誤"); //顯示錯誤信息
}
startPlayer(fileURL.toString()); //開始播放打開的文件
}
}
});
openURL.addActionListener(new ActionListener() { //打開URL按鈕事件處理
public void actionPerformed(ActionEvent event) {
String addressName =JOptionPane.showInputDialog(VideoPlayDemo.this, "輸入URL地址");
if (addressName != null)
startPlayer(addressName); //開始播放打開的URL
}
});
Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, Boolean.TRUE);
setSize(300, 200); //設(shè)置窗口大小
setVisible(true); //設(shè)置窗口為可視
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //關(guān)閉窗口時退出程序
}
//初始化播放器
public void startPlayer(String mediaLocation) {
if (player != null)
//如果播放器非空則移去先前的播放器組件
if (visualMedia != null)
container.remove(visualMedia); //如果對象visualMedia非空則移去
if (mediaControl != null) {
container.remove(mediaControl); //如果對象mediaControl非空則移去
player.close(); //關(guān)閉播放器
}
MediaLocator mediaLocator = new MediaLocator(mediaLocation); //媒體定位器
if (mediaLocator == null) {
showMessage("打開文件錯誤"); //顯示錯誤信息
return;
}
try {
player = Manager.createPlayer(mediaLocator); //得到播放器實例
player.addControlle
視頻也是一個文件,就用file類就可以了,這里簡單幫你實現(xiàn)一下吧:
---------------------------
//向該方法傳遞視頻文件的路徑
public static void getVideo(String filePath){
//得到一個File 對象
File fileImg=FraudinfoAction.getFraudImg(path);
//渲染一個二進制字節(jié)碼文件
renderBinary(fileImg);
}
在后臺,java中根據(jù)路徑找目錄下所有的視頻文件,然后把名字和url返回到前臺,前臺顯示列表,點擊后打開播放頁,網(wǎng)上很多flash制作的播放器,嵌入到網(wǎng)頁中并把視頻url傳遞進去就可以了。博客noday點虐 我若有時間就寫個例子