流媒体框架vlc简单的案例

这是我自己写的

package com.T;

import java.awt.EventQueue;

public class AA {

	private JFrame frame;
	private EmbeddedMediaPlayerComponent playerComponent;   //媒体播放器组件  
	JPanel  JP=new JPanel();
	JButton btnNewButton = new JButton("New button");
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				AA window = new AA();
				window.frame.setVisible(true);
			}
		});
	}

	public AA() {
		initialize();
	}

	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JP.setLayout(null);
		playerComponent=new EmbeddedMediaPlayerComponent();
		playerComponent.setBounds(165, 5, 104, 94);
		JP.add(playerComponent);
		frame.getContentPane().add(JP);


		btnNewButton.setBounds(130, 204, 93, 23);
		JP.add(btnNewButton);
		event();
	}
	private void event(){
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				File file=new File("C:\\Users\\Public\\Videos\\Sample Videos\\a.wmv");
				//playerComponent.getMediaPlayer().prepareMedia("C:\\Users\\Public\\Videos\\Sample Videos\\a.wmv");
				playerComponent.getMediaPlayer().playMedia(file.getAbsolutePath());
				playerComponent.getMediaPlayer().play();
			}
		});
	}
}
别人的案例 是这样的

package com.T;


import java.awt.EventQueue;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.SwingWorker;

import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;

import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;  
  
  
public class PlayerMain { 
  
   // private static final String RuntimeUtil = null;
	static Window frame;  
    //private static final String NATIVE_LIBRARY_SEARCH_PATH = "D:\\Program Files\\VideoLAN\\VLC\\sdk\\lib";  
  
    public static void main(String[] args) {  
          
        //环境配置,将vlc sdk导入到eclipse  
          
        //if(RuntimeUtil.isWindows()){                  }  
        //NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "D:\\Program Files\\VideoLAN\\VLC\\sdk\\lib");  //导入的路径是vlc的安装路径  
        //Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(),LibVlc.class);  
        //System.out.println(LibVlc.INSTANCE.libvlc_get_version());  
  
        //创建主程序界面运行窗体  
        EventQueue.invokeLater(new Runnable() {  
              
            @Override  
            public void run() {  
                try{  
                    frame=new Window();  
                    frame.setVisible(true);  
                    //frame.getMediaPlayer().playMedia("F:\\Videos\\Let.mp4");  //直接播放视屏,参数是视屏文件的绝对路径  
                    frame.getMediaPlayer().prepareMedia("C:\\Users\\Public\\Videos\\Sample Videos\\a.wmv");       //控制播放视屏  
                    new SwingWorker() {  
  
                        @Override  
                        protected String doInBackground() throws Exception {  
                            while (true) {  //获取视频播放进度并且按百分比显示  
                                long total=frame.getMediaPlayer().getLength();  
                                long curr=frame.getMediaPlayer().getTime();  
                                float percent=(float)curr/total;  
                                publish((int)(percent*100));  
                                Thread.sleep(100);  
                            }     
                            //return null;  
                        }  
                          
                        protected void process(java.util.List chunks) {  
                            for(int v:chunks){  
                                frame.getProgressBar().setValue(v);  
                            }   
                        }  
                    }.execute();  
                }catch(Exception e){  
                    e.printStackTrace();  
                }  
            }  
        });  
    }  
      
    //打开文件  
    public static void openVideo() {  
        JFileChooser chooser=new JFileChooser();  
        int v=chooser.showOpenDialog(null);  
        if(v==JFileChooser.APPROVE_OPTION){  
            File file=chooser.getSelectedFile();  
    		System.out.println(file.getAbsolutePath());

            frame.getMediaPlayer().playMedia(file.getAbsolutePath());  
        }  
    }  
      
    //退出播放  
    public static void exit() {  
        frame.getMediaPlayer().release();  
        System.exit(0);  
    }  
  
    //实现播放按钮的方法  
    public static void play() {  
        frame.getMediaPlayer().play();  
    }  
      
    //实现暂停按钮的方法  
    public static void pause() {  
        frame.getMediaPlayer().pause();  
    }  
      
    //实现停止按钮的方法  
    public static void stop() {  
        frame.getMediaPlayer().stop();  
    }  
      
    //实现点击进度条跳转的方法  
    public static void jumpTo(float to) {  
        frame.getMediaPlayer().setTime((long)(to*frame.getMediaPlayer().getLength()));  
    }  
      
    //实现控制声音的方法  
    public static void setVol(int v) {  
        frame.getMediaPlayer().setVolume(v);  
    }  
      
} 

package com.T;

import java.awt.BorderLayout;  
import java.awt.EventQueue;  
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import java.awt.event.MouseAdapter;  
import java.awt.event.MouseEvent;  
import java.awt.event.MouseListener;  
import java.beans.PropertyChangeEvent;  
import java.beans.PropertyChangeListener;  
  
import javax.swing.JButton;  
import javax.swing.JFrame;  
import javax.swing.JMenu;  
import javax.swing.JMenuBar;  
import javax.swing.JMenuItem;  
import javax.swing.JPanel;  
import javax.swing.JProgressBar;  
import javax.swing.JSlider;  
import javax.swing.border.EmptyBorder;  
import javax.swing.event.ChangeEvent;  
import javax.swing.event.ChangeListener;

import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;  
  
  
public class Window extends JFrame{  
      
    //protected static final String PlayerMain = null;
	private JPanel contentPane; //顶层容器,整个播放页面的容器  
    private JMenuBar menuBar;   //菜单栏  
    private JMenu mnFile,mnSetting,mnHelp;  //文件菜单  
    private JMenuItem mnOpenVideo,mnExit;   //文件菜单子目录,打开视屏、退出  
    private JPanel panel;   //控制区域容器  
    private JProgressBar progress;  //进度条  
    private JPanel progressPanel;   //进度条容器  
    private JPanel controlPanel;    //控制按钮容器  
    private JButton btnStop,btnPlay,btnPause;   //控制按钮,停止、播放、暂停  
    private JSlider slider;     //声音控制块  
      
      
    EmbeddedMediaPlayerComponent playerComponent;   //媒体播放器组件  
      
    public static void main(String[] args) {  
          
    }  
  
    //MainWindow构造方法,创建视屏播放的主界面  
    public Window(){  
        setTitle("   VideoPlayer   Copyright@2015  by 南柯一梦");  
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        setBounds(200,80,900,600);  
        contentPane=new JPanel();  
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));  
        contentPane.setLayout(new BorderLayout(0,0));  
        setContentPane(contentPane);  
          
          
        /*视频播放窗口中的菜单栏*/  
        menuBar=new JMenuBar();  
        setJMenuBar(menuBar);  
          
        mnFile=new JMenu("文件");     //设置菜单名  
        menuBar.add(mnFile);  
        mnSetting=new JMenu("设置");  
        menuBar.add(mnSetting);  
        mnHelp=new JMenu("帮助");  
        menuBar.add(mnHelp);  
          
        mnOpenVideo =new JMenuItem("打开文件"); //设置文件菜单子目录打开文件  
        mnFile.add(mnOpenVideo);  
          
        mnExit =new JMenuItem("退出");    //设置文件菜单子目录退出  
        mnFile.add(mnExit);  
          
        //打开文件  
        mnOpenVideo.addActionListener(new ActionListener() {  
              
            @Override  
            public void actionPerformed(ActionEvent e) {  
                PlayerMain.openVideo();  
            }  
        });  
          
        //退出  
        mnExit.addActionListener(new ActionListener() {  
              
            @Override  
            public void actionPerformed(ActionEvent e) {  
                PlayerMain.exit();  
            }  
        });  
          
          
        /*视屏窗口中播放界面部分*/  
        JPanel videoPane=new JPanel();  
        contentPane.add(videoPane, BorderLayout.CENTER);  
        videoPane.setLayout(new BorderLayout(0,0));  
          
        playerComponent=new EmbeddedMediaPlayerComponent();  
        videoPane.add(playerComponent);
          
        /*视屏窗口中控制部分*/  
          
        panel=new JPanel();     //实例化控制区域容器  
        videoPane.add(panel,BorderLayout.SOUTH);  
          
        progressPanel=new JPanel(); //实例化进度条容器  
        panel.add(progressPanel, BorderLayout.NORTH);  
  
        //添加进度条  
        progress=new JProgressBar();      
        progressPanel.add(progress);  
        //panel.add(progress,BorderLayout.NORTH);  
        progress.addMouseListener(new MouseAdapter() {  
            @Override  
            public void mouseClicked(MouseEvent e){     //点击进度条调整视屏播放进度  
                int x=e.getX();  
                PlayerMain.jumpTo((float)x/progress.getWidth());  
            }  
        });  
        progress.setStringPainted(true);  
          
          
        controlPanel=new JPanel();      //实例化控制按钮容器  
        panel.add(controlPanel,BorderLayout.SOUTH);  
          
        //添加停止按钮  
        btnStop=new JButton("停止");  
        btnStop.addMouseListener(new MouseAdapter() {  
            @Override  
            public void mouseClicked(MouseEvent e) {  
                PlayerMain.stop();  
            }  
        });  
        controlPanel.add(btnStop);  
          
        //添加播放按钮  
        btnPlay=new JButton("播放");  
        btnPlay.addMouseListener(new MouseAdapter() {  
            @Override  
            public void mouseClicked(MouseEvent e) {  
                PlayerMain.play();  
            }  
        });  
        controlPanel.add(btnPlay);  
          
        //添加暂停按钮  
        btnPause=new JButton("暂停");  
        btnPause.addMouseListener(new MouseAdapter() {  
            @Override  
            public void mouseClicked(MouseEvent e) {  
                PlayerMain.pause();  
            }  
        });  
        controlPanel.add(btnPause);  
          
        //添加声音控制块  
        slider=new JSlider();  
        slider.setValue(80);  
        slider.setMaximum(100);  
        slider.addChangeListener(new ChangeListener() {  
              
            @Override  
            public void stateChanged(ChangeEvent e) {  
                PlayerMain.setVol(slider.getValue());  
            }  
        });  
        controlPanel.add(slider);  
    }  
      
    //获取播放媒体实例(某个视频)  
    public EmbeddedMediaPlayer getMediaPlayer() {  
        return playerComponent.getMediaPlayer();  
    }  
      
    //获取进度条实例  
    public JProgressBar getProgressBar() {  
        return progress;  
    }  
      
}  

文件结构

vlc的jar包从网上下载的,里面有一些没用的还有重复的..懒得删了侧和看吧

你可能感兴趣的:(JavaSE)