-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudioPlayer.java
170 lines (148 loc) · 7.04 KB
/
AudioPlayer.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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package smpaint_3;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.media.Time;
import javax.swing.JOptionPane;
import javax.swing.Timer;
/**
* Reproductor de sonidos
* @author Nikolai
*/
public class AudioPlayer extends MPlayer {
//Variables
Timer timer;
double dubTime;
//Constructor
/**
* Inicializa el reproductor y empieza la reproducción
* @param padre MainWindow
* @param f Fichero de video
*/
public AudioPlayer(javax.swing.JFrame padre, File f) { super(padre, f, true);
initComponents();
int delay = 1000;
timer = new Timer(delay, taskPerformer);
timer.start();
dubTime = 0;
Date date = new Date((long)(dubTime*1000));
String formattedTime = new SimpleDateFormat("mm:ss").format(date);
timeLabel.setText(formattedTime);
durationSlider.setMaximum((int)p.getDuration().getSeconds());
}
//Manejador de eventos del timer
ActionListener taskPerformer = new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt){
dubTime = p.getMediaTime().getSeconds();
durationSlider.setValue((int)dubTime);
Date date = new Date((long)(dubTime*1000));
String formattedTime = new SimpleDateFormat("mm:ss").format(date);
timeLabel.setText(formattedTime);
repaint();
}
};
//Métodos
/**
* Mata elegantemente al reproductor
*/
public void Kill() {
try{
p.stop();
p.close();
timer.stop();
} catch(Exception e) {JOptionPane.showMessageDialog(null, e.toString());}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
playButton = new javax.swing.JButton();
durationSlider = new javax.swing.JSlider();
pauseButton = new javax.swing.JButton();
timeLabel = new javax.swing.JLabel();
playButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/smpaint_3/play.png"))); // NOI18N
playButton.setBorderPainted(false);
playButton.setFocusPainted(false);
playButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
playButtonActionPerformed(evt);
}
});
durationSlider.setValue(0);
durationSlider.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
durationSliderMouseReleased(evt);
}
});
pauseButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/smpaint_3/pause.png"))); // NOI18N
pauseButton.setBorderPainted(false);
pauseButton.setFocusPainted(false);
pauseButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
pauseButtonActionPerformed(evt);
}
});
timeLabel.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(durationSlider, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(playButton, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(pauseButton, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(timeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(12, 12, 12)
.addComponent(durationSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(playButton, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(pauseButton, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(timeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(11, 11, 11))
);
}// </editor-fold>//GEN-END:initComponents
private void playButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_playButtonActionPerformed
// TODO add your handling code here:
try{
p.start();
} catch(Exception e) {JOptionPane.showMessageDialog(null, e.toString());}
}//GEN-LAST:event_playButtonActionPerformed
private void pauseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pauseButtonActionPerformed
// TODO add your handling code here:
try{
p.stop();
} catch(Exception e) {JOptionPane.showMessageDialog(null, e.toString());}
}//GEN-LAST:event_pauseButtonActionPerformed
private void durationSliderMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_durationSliderMouseReleased
// TODO add your handling code here:
try{
p.setMediaTime(new Time((double)durationSlider.getValue()));
} catch(Exception e) {JOptionPane.showMessageDialog(null, e.toString());}
}//GEN-LAST:event_durationSliderMouseReleased
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JSlider durationSlider;
private javax.swing.JButton pauseButton;
private javax.swing.JButton playButton;
private javax.swing.JLabel timeLabel;
// End of variables declaration//GEN-END:variables
}