-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMediaPlayer.java
129 lines (100 loc) · 3.4 KB
/
MediaPlayer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package beta;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
public class MediaPlayer {
private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
private JFrame mediaFrame = new JFrame();
private String mediaPath = "";
private boolean pause = true;
MediaPlayer() {
NativeLibrary.addSearchPath(
RuntimeUtil.getLibVlcLibraryName(), "/Applications/vlc-2.0.0/VLC.app/Contents/MacOS/lib"
);
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
//Make media player
mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
final EmbeddedMediaPlayer video = mediaPlayerComponent.getMediaPlayer();
JPanel panel = new JPanel(new BorderLayout());
panel.add(mediaPlayerComponent, BorderLayout.CENTER);
mediaFrame.setContentPane(panel);
//add pause button and a progress bar
final JToggleButton pauseBtn = new JToggleButton("| |");
JPanel secondPanel = new JPanel(new GridLayout(2,1));
panel.add(secondPanel, BorderLayout.SOUTH);
final JProgressBar pb = new JProgressBar();
pb.setValue(0);
secondPanel.add(pb);
pauseBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
video.pause();
}
});
JPanel btnPanel = new JPanel(); //make panel to host all the control buttons
//make skip button
JButton btnSkip = new JButton(">>");
btnSkip.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
video.skip(5000);
}
});
//make skipback button
JButton btnSkipBack = new JButton("<<");
btnSkipBack.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
video.skip(-5000);
}
});
btnPanel.add(btnSkipBack);
btnPanel.add(pauseBtn);
btnPanel.add(btnSkip);
secondPanel.add(btnPanel);
//Progress Bar shows how long the video has been playing.
//The timer's actionlistener updates the progress bar
Timer timer = new Timer(500, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
long time = (long) video.getTime();
long totalTime = (long) video.getLength();
long percentage = ( (time * 100) / totalTime );
int n = (int) percentage;
pb.setValue(n);
}
});
timer.start();
mediaPath = "final.avi";
//Video stops playing after frame closes
mediaFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
video.stop();
mediaFrame.dispose();
}
});
mediaFrame.setLocation(100, 100);
mediaFrame.setSize(1050, 600);
mediaFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
mediaFrame.setVisible(true);
video.playMedia(mediaPath);
}
}