VLC Player

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

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

//import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.binding.LibVlcConst;
import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;

public class VLCPlayer {

//This is the path for libvlc.dll

private static File ourfile = null;
private MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
private EmbeddedMediaPlayer mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();

// ボリュームスライダ
private JSlider volumeSlider = new JSlider();
// ボリュームスライダ用のリスナー
ChangeListener volumeSliderCl;

// 演奏位置スライダ
private JSlider js = new JSlider();
// 演奏位置スライダ用のリスナー
ChangeListener JSliderCl;

// 時間表示
private JLabel lbl_time = new JLabel("( / )");

/**
* @wbp.parser.entryPoint
*/
public static void main(String[] args) {

NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:\\Program Files\\VideoLAN\\VLC");
NativeLibrary.addSearchPath("libvlc","C:\\JarLib\\LWJGL\\libvlc");
NativeLibrary.addSearchPath("libvlccore","C:\\JarLib\\LWJGL\\libvlccore");
//Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);

SwingUtilities.invokeLater(() -> {

VLCPlayer vlcPlayer = new VLCPlayer();

});

}

/**
* @wbp.parser.entryPoint
*/
private VLCPlayer() {

//MAXIMIZE TO SCREEN

//java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();

JFrame frame = new JFrame("vlcPlayer");
frame.setLocation(100, 100);
frame.setSize(1920, 1080);//1280,750
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

Canvas c = new Canvas();
c.setBackground(Color.black);
c.setBounds(100, 50, 1920,950);//1280,640

JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(c, BorderLayout.CENTER);
//p.setBounds(100, 50, 1280, 680);
frame.getContentPane().add(p, BorderLayout.NORTH);

JPanel p0 = new JPanel();
p0.setBounds( 100, 700, 1920, 30);//1280,30
//p0.setBackground(Color.gray);
frame.getContentPane().add(p0, BorderLayout.WEST);

JPanel p1 = new JPanel();
//p1.setBounds(100,740, 1280, 30);
p1.setBackground(Color.lightGray);
frame.getContentPane().add(p1, BorderLayout.SOUTH);

JButton selbutton = new JButton();
//selbutton.setIcon(new ImageIcon("C:/Users/biznis/Desktop/Newspaper/sangbadpratidin/d/pause.png"));
selbutton.setText("選  択");
selbutton.setBounds(20, 50, 150, 26);
p1.add(selbutton);

JButton playbutton = new JButton();
//playbutton.setIcon(new ImageIcon("C:/Users/biznis/Desktop/Newspaper/sangbadpratidin/d/play.png"));
// this.setSize(800,450);
playbutton.setText("開  始");
playbutton.setBounds(10, 50, 150, 26);
// playbutton.addActionListener((ActionListener) this);
p1.add(playbutton);

JButton pausebutton = new JButton();
//pausebutton.setIcon(new ImageIcon("C:/Users/biznis/Desktop/Newspaper/sangbadpratidin/d/pause.png"));
pausebutton.setText("一時停止");
pausebutton.setBounds(80, 50, 150, 26);
p1.add(pausebutton);

JButton stopbutton = new JButton();
//selbutton.setIcon(new ImageIcon("C:/Users/biznis/Desktop/Newspaper/sangbadpratidin/d/pause.png"));
stopbutton.setText("停  止");
stopbutton.setBounds(20, 50, 150, 26);
p1.add(stopbutton);

JButton endbutton = new JButton();
//selbutton.setIcon(new ImageIcon("C:/Users/biznis/Desktop/Newspaper/sangbadpratidin/d/pause.png"));
endbutton.setText("終  了");
endbutton.setBounds(20, 50, 150, 26);
p1.add(endbutton);

//JSlider volumeSlider = new JSlider();
volumeSlider.setMinimum(LibVlcConst.MIN_VOLUME);
volumeSlider.setMaximum(LibVlcConst.MAX_VOLUME);
volumeSlider.setPreferredSize(new Dimension(100, 26));
volumeSlider.setToolTipText("Change volume");
volumeSliderCl = getVolChangeListener();
volumeSlider.addChangeListener(volumeSliderCl);
p1.add(volumeSlider);

js.setPreferredSize(new Dimension(1150, 26));
p0.add(js);

//lbl_time.setBounds(20, 50, 150, 26);
p0.add(lbl_time);

js.addMouseMotionListener((MouseMotionListener) new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
if (js.getValue() / 100 < 1) {
mediaPlayer.setPosition((float) js.getValue() / 100);

}
}
});

Timer timer = new Timer(1000, new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
//System.out.println("movie pixmap created : " + mediaPlayer.getTime() + " / " + mediaPlayer.getLength());
js.setValue(Math.round(mediaPlayer.getPosition() * 100));
if (mediaPlayer.getLength()>0) {
//lbl_time.setText("(" + Math.round(mediaPlayer.getPosition() * 1000)+ "/" + "1000)");
lbl_time.setText("(" + mediaPlayer.getTime()/1000/60 + ":" + mediaPlayer.getTime()/1000%60 + "/" + mediaPlayer.getLength()/1000/60 + ":" + mediaPlayer.getLength()/1000%60 + ")");
}
}

});

timer.start();

pausebutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
mediaPlayer.pause();
// or mediaPlayer.pause() depending on what works.
final long time = mediaPlayer.getTime();
System.out.println(time);
}
});

playbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
mediaPlayer.play();
}
});

JFileChooser filechooser = new JFileChooser();
selbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {

filechooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
filechooser.showSaveDialog(null);
ourfile = filechooser.getSelectedFile();
String mediaPath = ourfile.getAbsolutePath();

//EmbeddedMediaPlayer mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
mediaPlayer.setVideoSurface(mediaPlayerFactory.newVideoSurface(c));

mediaPlayer.playMedia(mediaPath);

}
});

stopbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
mediaPlayer.stop();
}
});

endbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.exit(0);

}
});

}

// スライダの変更通知を受け取るリスナーです
private ChangeListener getVolChangeListener() {
return new ChangeListener() {
// スライダを操作すると通知が来ます
@Override
public void stateChanged(ChangeEvent e) {
// マウスで操作中は何もません
if (!volumeSlider.getValueIsAdjusting()) {
// マウスで操作中でなければスライダの位置に合わせてシークします
setGain();
}
}
};
}

// スライダに合わせてボリュームを設定します
private void setGain() {
try {
mediaPlayer.setVolume((int) volumeSlider.getValue() * 200 / volumeSlider.getMaximum());
} catch (Exception e1) {
e1.printStackTrace();
}
}

}

PAGE TOP