Demo entry 6644197

1

   

Submitted by 1 on Oct 02, 2017 at 08:59
Language: Java. Code size: 5.8 kB.

package 视频播放器;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.List;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
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.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.discovery.NativeDiscovery;
import uk.co.caprica.vlcj.player.MediaPlayer;
import video.VideoDir;
import video.VideoService;
import video.VideoServiceService;

public class Window extends JFrame {

	private final JFrame frame;

	private JMenuBar menuBar; // 菜单栏
	private JMenu mnFile, mnSetting, mnHelp, service; // 文件菜单
	private JMenuItem mnOpenVideo, mnExit;
	private final EmbeddedMediaPlayerComponent mediaPlayerComponent;

	private final JButton pauseButton;

	private final JButton rewindButton;

	private final JButton skipButton;

	private final JPanel ProgressPanel;

	private final JProgressBar progress;

	private final JSlider slider;

	private String dir;
	private int i;
	private int length = 0;
	private String[] videoName;

	public static void main(final String[] args) {

	}

	private void getVideoName() {
		VideoServiceService vvs = new VideoServiceService();
		VideoService vs = vvs.getVideoServicePort();
		VideoDir vd = vs.getVideodir();
		dir = vd.getDir();
		System.out.println(dir);
		File fdir = new File(dir);
		File f[] = fdir.listFiles();
		videoName = new String[f.length];
		i = 0;
		for (File m : f) {
			videoName[i++] = m.getName();
			length++;
			System.out.println(length);
		}

	}

	public Window() {
		frame = new JFrame("视频播放");
		frame.setBounds(100, 100, 800, 600);
		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				mediaPlayerComponent.release();
				System.exit(0);
			}
		});
		menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		mnFile = new JMenu("文件"); // 设置菜单名
		menuBar.add(mnFile);
		mnSetting = new JMenu("设置");
		menuBar.add(mnSetting);
		mnHelp = new JMenu("帮助");
		menuBar.add(mnHelp);
		service = new JMenu("服务");
		menuBar.add(service);
		getVideoName();
		for (int i = 0; i < length; i++) {
			service.add(videoName[i]);
		}
		mnOpenVideo = new JMenuItem("打开文件"); // 设置文件菜单子目录打开文件
		mnFile.add(mnOpenVideo);

		mnExit = new JMenuItem("退出"); // 设置文件菜单子目录退出
		mnFile.add(mnExit);
		mnOpenVideo.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				mainPlay.openVideo();
			}
		});

		// 退出
		mnExit.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				mainPlay.exit();
			}
		});
		for (i = 0; i < length; i++) {
			service.getItem(i).addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub

					mainPlay.openVideo(dir + "/" + e.getActionCommand());
				}
			});
		}
		JPanel contentPane = new JPanel();
		contentPane.setLayout(new BorderLayout());
		mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
		contentPane.add(menuBar, BorderLayout.NORTH);

		contentPane.add(mediaPlayerComponent, BorderLayout.CENTER);

		progress = new JProgressBar();
		ProgressPanel = new JPanel();
		ProgressPanel.add(progress);
		progress.setStringPainted(true);
		JPanel controlsPane = new JPanel();
		controlsPane.add(ProgressPanel);
		pauseButton = new JButton("暂停");
		controlsPane.add(pauseButton);
		rewindButton = new JButton("后退");
		controlsPane.add(rewindButton);
		skipButton = new JButton("前进");
		controlsPane.add(skipButton);
		slider = new JSlider();
		slider.setValue(80);
		slider.setMaximum(100);
		slider.setVisible(true);
		controlsPane.add(slider);
		contentPane.add(controlsPane, BorderLayout.SOUTH);
		progress.addMouseListener(new MouseAdapter() {

			public void mouseClicked(MouseEvent e) { // 点击进度条调整视屏播放进度
				int x = e.getX();
				mainPlay.jumpTo((float) x / progress.getWidth());
			}
		});
		progress.setStringPainted(true);
		pauseButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				mainPlay.pause();
			}
		});

		rewindButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				mainPlay.resume();
			}
		});

		skipButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				mainPlay.skip();
			}
		});

		frame.setContentPane(contentPane);
		frame.setVisible(true);

		slider.addChangeListener(new ChangeListener() {

			public void stateChanged(ChangeEvent e) {

				mainPlay.setVol(slider.getValue());
			}
		});
	}

	public MediaPlayer getMediaPlayer() {
		// TODO Auto-generated method stub
		return mediaPlayerComponent.getMediaPlayer();
	}

	public JProgressBar getProgerss() {
		return progress;
	}
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).