-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathJMediaPlayerControlFrame.java
166 lines (157 loc) · 5.71 KB
/
JMediaPlayerControlFrame.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import org.datavyu.plugins.MediaPlayer;
import org.datavyu.plugins.MediaPlayerWindow;
import org.datavyu.plugins.PlaybackRateController;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class JMediaPlayerControlFrame extends JFrame implements KeyListener, ChangeListener {
private MediaPlayer mediaPlayer;
private JSlider jSlider;
JMediaPlayerControlFrame(MediaPlayer mediaPlayer) {
this.mediaPlayer = mediaPlayer;
jSlider = new JSlider(0, (int) mediaPlayer.getDuration(), 0);
jSlider.setFocusable(false);
jSlider.addChangeListener(this);
add(jSlider, BorderLayout.SOUTH);
addKeyListener(this);
pack();
setSize(640, 480);
setVisible(true);
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) {
double currentTime, nextTime;
float currentVolume, nextVolume;
float currentRate, nextRate;
int frameNB;
double step = (1.0 / mediaPlayer.getFps());
currentRate = mediaPlayer.getRate();
switch (e.getKeyCode()) {
case KeyEvent.VK_NUMPAD5:
System.out.println("Stop");
mediaPlayer.stop();
break;
case KeyEvent.VK_NUMPAD8:
System.out.println("Play");
mediaPlayer.play();
break;
case KeyEvent.VK_NUMPAD2:
System.out.println("Pause");
mediaPlayer.pause();
break;
case KeyEvent.VK_S:
System.out.println("Step Forward");
mediaPlayer.stepForward();
break;
case KeyEvent.VK_D:
System.out.println("Step Backward");
mediaPlayer.stepBackward();
System.out.println("Finished backward step");
break;
case KeyEvent.VK_I:
System.out.println(
"Player State "
+ mediaPlayer.getState()
+ " Stream Information : Fps = "
+ mediaPlayer.getFps()
+ " Image Width = "
+ mediaPlayer.getImageWidth()
+ " Image Height = "
+ mediaPlayer.getImageHeight()
+ " Stream Duration = "
+ mediaPlayer.getDuration()
+ " Presentation Time = "
+ mediaPlayer.getPresentationTime());
break;
case KeyEvent.VK_NUMPAD6:
nextRate = PlaybackRateController.stepToFaster(currentRate);
System.out.println("Change rate from " + currentRate + " to " + nextRate);
mediaPlayer.setRate(nextRate);
break;
case KeyEvent.VK_NUMPAD4:
nextRate = PlaybackRateController.stepToSlower(currentRate);
System.out.println("Change rate from " + currentRate + " to " + nextRate);
mediaPlayer.setRate(nextRate);
break;
case KeyEvent.VK_SPACE:
System.out.println("Toggle Pause");
mediaPlayer.togglePause();
break;
case KeyEvent.VK_LEFT:
currentTime = mediaPlayer.getPresentationTime();
nextTime = currentTime - 1;
System.out.println("Seek from " + currentTime + " sec to " + nextTime + "sec");
mediaPlayer.seek(nextTime);
break;
case KeyEvent.VK_RIGHT:
currentTime = mediaPlayer.getPresentationTime();
nextTime = currentTime + 1;
System.out.println("Seek from " + currentTime + " sec to " + nextTime + "sec");
mediaPlayer.seek(nextTime);
break;
case KeyEvent.VK_UP:
currentTime = mediaPlayer.getPresentationTime();
nextTime = currentTime + 5;
System.out.println("Seek from " + currentTime + " sec to " + nextTime + "sec");
mediaPlayer.seek(nextTime);
break;
case KeyEvent.VK_DOWN:
currentTime = mediaPlayer.getPresentationTime();
nextTime = currentTime - 5;
System.out.println("Seek from " + currentTime + " sec to " + nextTime + "sec");
mediaPlayer.seek(nextTime);
break;
case KeyEvent.VK_0:
currentVolume = mediaPlayer.getVolume();
nextVolume = currentVolume - 0.1F;
System.out.println("Change volume from " + currentVolume + " dB to " + nextVolume + " dB");
mediaPlayer.setVolume(nextVolume);
break;
case KeyEvent.VK_9:
currentVolume = mediaPlayer.getVolume();
nextVolume = currentVolume + 0.1F;
System.out.println("Change volume from " + currentVolume + " dB to " + nextVolume + " dB");
mediaPlayer.setVolume(nextVolume);
break;
case KeyEvent.VK_M:
System.out.println("Mute");
mediaPlayer.setMute(true);
break;
case KeyEvent.VK_N:
System.out.println("Un-mute");
mediaPlayer.setMute(false);
break;
case KeyEvent.VK_H:
System.out.println("Hide Window");
((MediaPlayerWindow) mediaPlayer).hideWindow();
break;
case KeyEvent.VK_J:
System.out.println("Show Window");
((MediaPlayerWindow) mediaPlayer).showWindow();
break;
case KeyEvent.VK_ESCAPE:
mediaPlayer.dispose();
System.exit(0);
System.out.println("Dispose the media player");
default:
System.err.println("Unrecognized event " + e.paramString());
}
}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void stateChanged(ChangeEvent e) {
int newTime = jSlider.getValue();
double currentTime = mediaPlayer.getPresentationTime();
System.out.println("Seek from " + currentTime + " sec to " + newTime + "sec");
mediaPlayer.seek(newTime);
}
public void handleKeyEvents(final int javaKeyCode) {
this.dispatchEvent(new KeyEvent(this, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, javaKeyCode, KeyEvent.CHAR_UNDEFINED));
}
}