zoukankan      html  css  js  c++  java
  • Java 课程设计

    在这周 我们继续接着上周的工程配置做视频播放器 这周我们完成了视频播放器的播放 播放,暂停,停止按钮的添加和功能实现,菜单栏的添加和功能实现 按原计划第十四周的任务基本完成 在制作过程中遇到了不少难题 如libvlc无法导入 布局结构设计问题 进度条功能实现等等一系列问题 在老师的帮助下和我们组共同努力下 终于算是基本解决这些问题 也将这周需要完成的界面和功能实现 这周的成果在下方

    代码如下:
    (1)videoplayer.main包 PlayMain类
    import java.awt.EventQueue;
    import java.io.File;

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

    import com.sun.jna.NativeLibrary;

    import uk.co.caprica.vlcj.binding.LibVlc;
    import uk.co.caprica.vlcj.discovery.NativeDiscovery;
    import uk.co.caprica.vlcj.runtime.RuntimeUtil;
    import videoplayer.views.MainWindow;

    public class PlayMain {

    static MainWindow frame;
    
    public static void main(String[] args) {
    	if(RuntimeUtil.isMac()){
    	 NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(),"/home/vlc");
            System.out.println(LibVlc.INSTANCE.libvlc_get_version());
    	}else if(RuntimeUtil.isNix()){
    		 NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(),"/home/linux/vlc/install/lib");	
    		 System.out.println(LibVlc.INSTANCE.libvlc_get_version());
    	}else if(RuntimeUtil.isWindows()){
    		NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(),"C:\Program Files\VideoLAN\VLC");
    		System.out.println(LibVlc.INSTANCE.libvlc_get_version());
    	}
    	boolean found = new NativeDiscovery().discover();
        System.out.println(found);
        System.out.println(LibVlc.INSTANCE.libvlc_get_version());
        
        EventQueue.invokeLater(new Runnable() {
    		public void run() {
    			try {
    				 frame = new MainWindow();
    				frame.setVisible(true);
    				frame.getMediaPlayer().prepareMedia("E:\BaiduYunDownload\haskjdhaskjdhkasjdhkasjhdkjasd.mkv");
    				//frame.getMediaPlayer().playMedia("E:\BaiduYunDownload\haskjdhaskjdhkasjdhkasjhdkjasd.mkv");
    			
    				
    				new SwingWorker<String, Integer>() {
    
    					@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);
    						}
    					}
    
    					protected void process(java.util.List<Integer> chunks) {
    						for (int v : chunks) {
    							frame.getProgressBar().setValue(v);
    						}
    					}
    				}.execute();
    				
    				
    				
    				
    			
    				}catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    	});
            
    
    	
    }
    
    //播放方法
    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 openVideo() {
    	JFileChooser chooser = new JFileChooser();
    	int v = chooser.showOpenDialog(null);
    	if (v == JFileChooser.APPROVE_OPTION) {
    		File file = chooser.getSelectedFile();
    		frame.getMediaPlayer().playMedia(file.getAbsolutePath());
    	}
    }
    
    public static void openSubtitle() {
    	JFileChooser chooser = new JFileChooser();
    	int v = chooser.showOpenDialog(null);
    	if (v == JFileChooser.APPROVE_OPTION) {
    		File file = chooser.getSelectedFile();
    		frame.getMediaPlayer().setSubTitleFile(file);
    	}
    }
    
    public static void exit() {
    	frame.getMediaPlayer().release();
    	System.exit(0);
    }
    

    }

    (2)videoplayer.views包 MainWindow类
    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 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.border.EmptyBorder;

    import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
    import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
    import videoplayer.main.PlayMain;

    public class MainWindow extends JFrame {

    private JPanel contentPane;
    EmbeddedMediaPlayerComponent playerComponent;
    private JPanel panel;
    private JButton btnPlay;
    private JButton btnPause;
    private JButton btnStop;
    private JPanel controlPanel;
    private JProgressBar progress;
    private JMenuBar menuBar;
    private JMenu mnFile;
    private JMenuItem mntmOpenVideo;
    private JMenuItem mntmOpenSubtitle;
    private JMenuItem mntmExit;
    
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
    	
    }
    
    /**
     * Create the frame.
     */
    public MainWindow() {
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	setBounds(100, 100, 600, 400);
    	
    	menuBar = new JMenuBar();
    	setJMenuBar(menuBar);
    	mnFile = new JMenu("文件");
    	menuBar.add(mnFile);
    	
    	mntmOpenVideo = new JMenuItem("打开视频");
    	mnFile.add(mntmOpenVideo);
    	
    	mntmOpenSubtitle = new JMenuItem("导入字幕");
    	mnFile.add(mntmOpenSubtitle);
    	
    	mntmExit = new JMenuItem("退出");
    	mnFile.add(mntmExit);
    	
    	mntmOpenVideo.addActionListener(new ActionListener() {
    		
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			PlayMain.openVideo();
    		}
    	});
    	
    	mntmOpenSubtitle.addActionListener(new ActionListener() {
    		
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			PlayMain.openSubtitle();
    		}
    	});
    	
    	mntmExit.addActionListener(new ActionListener() {
    		
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			PlayMain.exit();
    		}
    	});
    	
    	
    	contentPane = new JPanel();
    	contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    	contentPane.setLayout(new BorderLayout(0, 0));
    	setContentPane(contentPane);
    	
    	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);
    	panel.setLayout(new BorderLayout(0, 0));
    	
    	controlPanel = new JPanel();
    	panel.add(controlPanel);
    	
    	btnStop = new JButton("停止");
    	controlPanel.add(btnStop);
    	
    	btnPlay = new JButton("播放");
    	controlPanel.add(btnPlay);
    	
    	btnPause = new JButton("暂停");
    	controlPanel.add(btnPause);
    	
    	
    	
    	
    	progress = new JProgressBar();
    	progress.addMouseListener(new MouseAdapter() {
    		@Override
    		public void mouseClicked(MouseEvent e) {
    			int x = e.getX();
    			PlayMain.jumpTo((float)x/progress.getWidth());
    		}
    	});
    	progress.setStringPainted(true);
    	panel.add(progress, BorderLayout.NORTH);
    	
    	
    	btnPause.addMouseListener(new MouseAdapter() {
    		@Override
    		public void mouseClicked(MouseEvent e) {
    			PlayMain.pause();
    		}
    	});
    	btnPlay.addMouseListener(new MouseAdapter() {
    		@Override
    		public void mouseClicked(MouseEvent arg0) {
    			PlayMain.play();
    		}
    	});
    	btnStop.addMouseListener(new MouseAdapter() {
    		@Override
    		public void mouseClicked(MouseEvent e) {
    			PlayMain.stop();
    		}
    	});
    }
    public EmbeddedMediaPlayer getMediaPlayer(){
    	return playerComponent.getMediaPlayer();
    }
    
    public JProgressBar getProgressBar() {
    	return progress;
    }
    

    }

    (二)下面是程序实现的图片
    (1)代码中指定的视频可以播放,可以通过播放,暂停,停止按钮进行控制,进度条可以实现进度变化

    (2)文件菜单栏下,打开视频,导出字幕,退出可以使用,有响应。

  • 相关阅读:
    关于WorkFlow的使用以及例子
    11 个用来创建图形和图表的 JavaScript 工具包
    产品经理看程序员的自我修养
    extern "C" 的作用
    DLL 演示
    C++中L和_T()之区别
    VMware:Configuration file was created by a VMware product with more features than this version
    使用内存映射来对文件排序
    平衡二叉树的插入删除操作
    volatile关键字的使用
  • 原文地址:https://www.cnblogs.com/abnry/p/5561172.html
Copyright © 2011-2022 走看看