1
1
/*
2
2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
3
*
4
- * Copyright 2019 Neil C Smith.
4
+ * Copyright 2021 Neil C Smith.
5
5
*
6
6
* Copying and distribution of this file, with or without modification,
7
7
* are permitted in any medium without royalty provided the copyright
14
14
import java .awt .BorderLayout ;
15
15
import java .awt .Dimension ;
16
16
import java .awt .EventQueue ;
17
+ import java .util .EnumSet ;
17
18
import java .util .concurrent .TimeUnit ;
18
19
19
20
import javax .swing .Box ;
26
27
import org .freedesktop .gstreamer .Bus ;
27
28
import org .freedesktop .gstreamer .Element ;
28
29
import org .freedesktop .gstreamer .ElementFactory ;
30
+ import org .freedesktop .gstreamer .Format ;
29
31
import org .freedesktop .gstreamer .Gst ;
30
32
import org .freedesktop .gstreamer .Structure ;
31
33
import org .freedesktop .gstreamer .elements .PlayBin ;
34
+ import org .freedesktop .gstreamer .event .SeekFlags ;
32
35
import org .freedesktop .gstreamer .message .Message ;
33
36
import org .freedesktop .gstreamer .message .MessageType ;
34
37
@@ -80,24 +83,26 @@ public static void main(String[] args) {
80
83
playButton .addActionListener (e -> playbin .play ());
81
84
JButton pauseButton = new JButton ("Pause" );
82
85
pauseButton .addActionListener (e -> playbin .pause ());
83
-
86
+
84
87
// position slider
85
88
JSlider position = new JSlider (0 , 1000 );
86
89
position .addChangeListener (e -> {
87
90
if (position .getValueIsAdjusting ()) {
88
- long dur = playbin .queryDuration (TimeUnit . NANOSECONDS );
89
- long pos = playbin .queryPosition (TimeUnit . NANOSECONDS );
91
+ long dur = playbin .queryDuration (Format . TIME );
92
+ long pos = playbin .queryPosition (Format . TIME );
90
93
if (dur > 0 ) {
91
94
double relPos = position .getValue () / 1000.0 ;
92
- playbin .seek ((long ) (relPos * dur ), TimeUnit .NANOSECONDS );
93
- }
95
+ playbin .seekSimple (Format .TIME ,
96
+ EnumSet .of (SeekFlags .FLUSH , SeekFlags .KEY_UNIT ),
97
+ (long ) (relPos * dur ));
98
+ }
94
99
}
95
100
});
96
101
// sync slider position to video when not dragging
97
102
new Timer (50 , e -> {
98
103
if (!position .getValueIsAdjusting ()) {
99
- long dur = playbin .queryDuration (TimeUnit . NANOSECONDS );
100
- long pos = playbin .queryPosition (TimeUnit . NANOSECONDS );
104
+ long dur = playbin .queryDuration (Format . TIME );
105
+ long pos = playbin .queryPosition (Format . TIME );
101
106
if (dur > 0 ) {
102
107
double relPos = (double ) pos / dur ;
103
108
position .setValue ((int ) (relPos * 1000 ));
@@ -129,9 +134,9 @@ public static void main(String[] args) {
129
134
130
135
@ Override
131
136
public void busMessage (Bus arg0 , Message message ) {
132
- Structure struct = message .getStructure ();
133
137
if (message .getType () == MessageType .ELEMENT
134
138
&& message .getSource ().getName ().startsWith ("level" )) {
139
+ Structure struct = message .getStructure ();
135
140
// We can get either rms or peak
136
141
double [] levels = struct .getDoubles ("peak" );
137
142
// Calculate the time offset required to get the level
@@ -141,6 +146,7 @@ public void busMessage(Bus arg0, Message message) {
141
146
() -> EventQueue .invokeLater (() -> updateLevelDisplay (levels )),
142
147
timeDelay , TimeUnit .NANOSECONDS );
143
148
}
149
+ message .dispose ();
144
150
}
145
151
146
152
private long getTimeOffset (Structure struct ) {
0 commit comments