Видеопроигрыватель Java&Xuggle
31.01.2012, 13:13. Показов 2494. Ответов 0
пишу видеопроигрыватель
помогите пожалуйста реализовать перемотку, либо ускоренное и замедленное воспроизведение для аудио и видео
примеры работы с библиотекой: http://wiki.xuggle.com/Tutorials
мой проект здесь:
http://ifolder.ru/28413175
Классы:
Screen
| 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
| package javaapplication3;
import com.xuggle.xuggler.IContainer;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JSlider;
public class Screen extends javax.swing.JFrame {
public static int SCREEN_CLICKED = 0;
protected Thread tv = null;
protected Thread ta = null;
protected Video v = null;
protected Audio a = null;
protected String filename;
protected long filesize;
private float speed = (float) 1.0;
private long position;
public Screen() {
initComponents();
setCenterPosition();
setMinimumSize(new Dimension(500, 152));
setSize(500, 500);
this.setVisible(true);
this.setTitle("Player");
}
protected final void setCenterPosition() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getPreferredSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
this.setLocation(screenSize.width / 6, screenSize.height / 6);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
screen = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
play = new javax.swing.JButton();
pause = new javax.swing.JButton();
slow = new javax.swing.JButton();
fast = new javax.swing.JButton();
time = new javax.swing.JProgressBar();
jSlider1 = new javax.swing.JSlider();
timel = new javax.swing.JLabel();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
exit = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
screen.setBackground(new java.awt.Color(0, 0, 0));
screen.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
screenMouseClicked(evt);
}
});
getContentPane().add(screen, java.awt.BorderLayout.NORTH);
play.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/Play1Normal.png"))); // NOI18N
play.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
playActionPerformed(evt);
}
});
pause.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/PauseNormal.png"))); // NOI18N
pause.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
pauseActionPerformed(evt);
}
});
slow.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/StepBackNormalOrange.png"))); // NOI18N
slow.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
slowActionPerformed(evt);
}
});
fast.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/StepForwardNormalBlue.png"))); // NOI18N
fast.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fastActionPerformed(evt);
}
});
time.setMinimum(0);
time.setMaximum(100);
jSlider1.setMinimum(0);
jSlider1.setMaximum(100);
jSlider1.setValue(0);
jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
jSlider1StateChanged(evt);
}
});
timel.setText("00:00:00");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(time, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(play)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(pause)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(slow)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(fast)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(timel, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 249, Short.MAX_VALUE))
.addComponent(jSlider1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(time, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(play)
.addComponent(pause)
.addComponent(slow)
.addComponent(fast)
.addComponent(timel))
.addContainerGap())
);
getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);
jMenu1.setText("Файл");
jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem1.setText("Открыть файл");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem1);
exit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
exit.setText("Выход");
exit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitActionPerformed(evt);
}
});
jMenu1.add(exit);
jMenuBar1.add(jMenu1);
setJMenuBar(jMenuBar1);
pack();
}// </editor-fold>
private void pauseActionPerformed(java.awt.event.ActionEvent evt) {
if (tv != null) {
ta.suspend();
tv.suspend();
}
}
private void screenMouseClicked(java.awt.event.MouseEvent evt) {
if (tv != null) {
if (SCREEN_CLICKED == 0) {
tv.suspend();
ta.suspend();
SCREEN_CLICKED = 1;
} else {
tv.resume();
ta.resume();
SCREEN_CLICKED = 0;
}
}
}
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
Toolkit.getDefaultToolkit().setDynamicLayout(true);
JFileChooser jfc = new JFileChooser("C:\\For player");
int ret = jfc.showOpenDialog(this);
if (ret == JFileChooser.APPROVE_OPTION) {
if (tv != null) {
System.out.println("No null");
tv.stop();
ta.stop();
}
speed = (float) 1.0;
filename = jfc.getSelectedFile().getAbsolutePath();
v = new Video(filename, screen, time, timel, (int)speed, 0);
tv = new Thread(v);
tv.setName("Video");
a = new Audio(filename, speed);
ta = new Thread(a);
ta.setName("Audio");
//jSlider1.setValue(0);
ta.start();
while (a.mLine == null);
tv.start();
filesize = v.fs;
setTitle(jfc.getSelectedFile().getName() + " - MyPlayer");
} else {
jfc.disable();
}
}
private void slowActionPerformed(java.awt.event.ActionEvent evt) {
speed/=2;
ta.stop();
tv.stop();
a = new Audio(filename, speed);
v = new Video(filename, screen, time, timel, 2, 1);
ta = new Thread(a);
tv = new Thread(v);
ta.start();
tv.start();
}
private void fastActionPerformed(java.awt.event.ActionEvent evt) {
if (speed * 2 <= 8.0) {
speed*=2;
ta.stop();
tv.stop();
a = new Audio(filename, speed);
v = new Video(filename, screen, time, timel, (int)speed, 0);
ta = new Thread(a);
tv = new Thread(v);
ta.start();
tv.start();
}
}
private void playActionPerformed(java.awt.event.ActionEvent evt) {
if (tv != null) {
ta.resume();
tv.resume();
}
}
private void exitActionPerformed(java.awt.event.ActionEvent evt) {
if (tv != null) {
tv.stop();
ta.stop();
dispose();
} else {
dispose();
}
}
private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {
if (tv != null) {
JSlider source = (JSlider) evt.getSource();
if (!source.getValueIsAdjusting())
{
ta.suspend();
tv.suspend();
int pos = (int) source.getValue();
position = (v.fs * pos) / 100;
a.container.seekKeyFrame(a.audioStreamId, 0, IContainer.SEEK_FLAG_BYTE);
v.container.seekKeyFrame(v.videoStreamId, 0, IContainer.SEEK_FLAG_BYTE);
a.positionA=position;
v.positionV=position;
a.closeJavaSound();
a.openJavaSound(a.audioCoder, speed);
ta.resume();tv.resume();
}
}
}
// Variables declaration - do not modify
private javax.swing.JMenuItem exit;
private javax.swing.JButton fast;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JPanel jPanel1;
protected javax.swing.JSlider jSlider1;
private javax.swing.JButton pause;
private javax.swing.JButton play;
protected javax.swing.JLabel screen;
private javax.swing.JButton slow;
protected javax.swing.JProgressBar time;
protected javax.swing.JLabel timel;
// End of variables declaration
} |
|
Video
| 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication3;
import com.xuggle.xuggler.*;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
public class Video implements Runnable {
private JLabel screen;
private JProgressBar pb;
private JLabel time;
private String filename;
protected long fs;
protected long positionV = -1;
protected IContainer container=null;
protected int videoStreamId = -1;
private int WIDTH, HEIGHT;
protected long picTime;
protected int coeff;
protected int flag;
public Video(String f, JLabel scr, JProgressBar p, JLabel tl, int c, int flg) {
filename = f;
screen = scr;
pb = p;
time = tl;
coeff=c;
flag=flg;
}
public void play() throws Exception {
try {
IVideoPicture picture;
if (!IVideoResampler.isSupported(IVideoResampler.Feature.FEATURE_COLORSPACECONVERSION)) {
throw new RuntimeException("could not find video stream in container: " + filename);
}
container = IContainer.make();
IStream stream;
IStreamCoder coder;
if (container.open(filename, IContainer.Type.READ, null) < 0) {
throw new IllegalArgumentException("could not open file: " + filename);
}
fs = container.getFileSize();
int numStreams = container.getNumStreams();
IStreamCoder videoCoder = null;
for (int i = 0; i < numStreams; i++) {
stream = container.getStream(i);
coder = stream.getStreamCoder();
if (coder.getCodecType() == ICodec.Type.CODEC_TYPE_VIDEO) {
videoStreamId = i;
videoCoder = coder;
break;
}
}
if (videoStreamId == -1) {
throw new RuntimeException("could not find video stream in container: " + filename);
}
if (videoCoder.open() < 0) {
throw new RuntimeException("could not open video decoder for container: " + filename);
}
IVideoResampler resampler = null;
IPacket packet = IPacket.make();
Main.n.setSize(videoCoder.getWidth(), videoCoder.getHeight() + 150);
long alldecode = 0;
long fsize = container.getFileSize();
while (container.readNextPacket(packet) >= 0) {
if (packet.getPosition() >= positionV)
{
if (packet.getStreamIndex() == videoStreamId)
{
WIDTH = Main.n.getWidth();
HEIGHT = Main.n.getHeight() - 150;
videoCoder.setWidth(WIDTH);
videoCoder.setHeight(HEIGHT);
picture = IVideoPicture.make(videoCoder.getPixelType(),
videoCoder.getWidth(), videoCoder.getHeight());
int offset = 0;
int bytesDecoded = 0;
while (offset < packet.getSize()) {
bytesDecoded = videoCoder.decodeVideo(picture, packet, offset);
if (bytesDecoded < 0) {
break;
}
offset += bytesDecoded;
alldecode += bytesDecoded;
if (picture.isComplete()) {
IVideoPicture newPic = picture;
if (videoCoder.getPixelType() != IPixelFormat.Type.BGR24) {
resampler = IVideoResampler.make(WIDTH,
HEIGHT, IPixelFormat.Type.BGR24,
videoCoder.getWidth(), videoCoder.getHeight(), videoCoder.getPixelType());
if (resampler == null) {
throw new RuntimeException("could not create color space resampler for: " + filename);
}
}
if (resampler != null) {
newPic = IVideoPicture.make(resampler.getOutputPixelFormat(),
WIDTH, HEIGHT);
if (resampler.resample(newPic, picture) < 0) {
throw new RuntimeException("could not resample video from: " + filename);
}
}
if (newPic.getPixelType() != IPixelFormat.Type.BGR24) {
throw new RuntimeException("could not decode video" + " as BGR 24 bit data in: " + filename);
}
BufferedImage javaImage = Utils.videoPictureToImage(newPic);
picTime = newPic.getTimeStamp() / 1000;
long mPos = Main.n.a.mLine.getMicrosecondPosition() / 1000;
if (picTime > mPos && mPos > 0)
{
long ttsleep=0;
if(flag==0)
{
ttsleep = picTime - mPos*coeff;
if (ttsleep > 0) {
Thread.sleep(ttsleep);
}
}
if(flag==1)
{
ttsleep = picTime-mPos;
if (ttsleep > 0) {
Thread.sleep(ttsleep);
}
}
}
updateJavaWindow(javaImage);
time.setText(newPic.getFormattedTimeStamp().substring(0, 8));
pb.setValue((int) ((alldecode * 100) / fsize));
}
}
if (bytesDecoded < 0) {
break;
}
}
}
}
if (videoCoder != null) {
videoCoder.close();
}
if (container != null) {
container.close();
}
} catch (RuntimeException e) {
e.printStackTrace();
//JOptionPane.showMessageDialog(Main.n, "Unfortunately, such happens.\n", "Error", JOptionPane.ERROR_MESSAGE);
Thread.currentThread().stop();
}
}
private void updateJavaWindow(BufferedImage javaImage) throws InterruptedException {
screen.setIcon(new ImageIcon(javaImage));
}
@Override
public void run() {
try {
play();
} catch (Exception ex) {
Logger.getLogger(Video.class.getName()).log(Level.SEVERE, null, ex);
}
}
} |
|
Audio
| 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
| package javaapplication3;
import com.xuggle.xuggler.*;
import javax.sound.sampled.*;
public class Audio implements Runnable {
private String filename;
private float speedA;
protected SourceDataLine mLine;
protected IStreamCoder audioCoder = null;
protected long positionA = -1;
protected IContainer container=null;
protected int audioStreamId = -1;
protected IAudioSamples samples;
public Audio(String f, float speed) {
filename = f;
speedA = speed;
}
public void play() {
container = IContainer.make();
if (container.open(filename, IContainer.Type.READ, null) < 0) {
throw new IllegalArgumentException("could not open file: " + filename);
}
int numStreams = container.getNumStreams();
IStream stream = null;
for (int i = 0; i < numStreams; i++) {
stream = container.getStream(i);
IStreamCoder coder = stream.getStreamCoder();
if (coder.getCodecType() == ICodec.Type.CODEC_TYPE_AUDIO) {
audioStreamId = i;
audioCoder = coder;
break;
}
}
if (audioStreamId == -1) {
throw new RuntimeException("could not find audio stream in container: " + filename);
}
if (audioCoder.open() < 0) {
throw new RuntimeException("could not open audio decoder for container: " + filename);
}
openJavaSound(audioCoder, speedA);
IPacket packet = IPacket.make();
while(container.readNextPacket(packet) >= 0)
{
int offset = 0;
int bytesDecoded=0;
if(packet.getPosition()>=positionA)
{
if (packet.getStreamIndex() == audioStreamId)
{
samples = IAudioSamples.make(1024, audioCoder.getChannels());
while(offset < packet.getSize())
{
bytesDecoded = audioCoder.decodeAudio(samples, packet, offset);
if (bytesDecoded < 0)
break;
offset += bytesDecoded;
if (samples.isComplete())
{
playJavaSound(samples);
}
}
if(bytesDecoded<0){break;}
}
}
}
closeJavaSound();
if (audioCoder != null) {
audioCoder.close();
audioCoder = null;
}
if (container != null) {
container.close();
container = null;
}
}
protected void openJavaSound(IStreamCoder aAudioCoder, float speed) {
AudioFormat audioFormat = new AudioFormat(aAudioCoder.getSampleRate() * speed,
(int) IAudioSamples.findSampleBitDepth(aAudioCoder.getSampleFormat()),
aAudioCoder.getChannels(),
true,
false);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
try {
if (mLine != null) {
closeJavaSound();
}
mLine = (SourceDataLine) AudioSystem.getLine(info);
mLine.open(audioFormat);
mLine.start();
} catch (LineUnavailableException e) {
throw new RuntimeException("could not open audio line");
}
}
private void playJavaSound(IAudioSamples aSamples) {
byte[] rawBytes = aSamples.getData().getByteArray(0, aSamples.getSize());
mLine.write(rawBytes, 0, aSamples.getSize());
}
protected void closeJavaSound() {
if (mLine != null) {
//mLine.drain();
mLine.close();
mLine = null;
}
}
@Override
public void run() {
play();
}
} |
|
|