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
|
import java.awt.EventQueue;
import javax.sound.sampled.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.util.Timer;
/**
* @author serabile
*
*/
public class JavaSound extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private BooleanControl mute;
private Timer timer;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JavaSound frame = new JavaSound();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public BooleanControl getMute() {
return mute;
}
public void setMute(BooleanControl mute) {
this.mute = mute;
}
/**
* Create the frame.
*/
static AudioFormat positionneFormatAudio() {
float sampleRate = 8000;
int sampleSizeInBits = 8;
int channels = 1;
boolean signed = true;
boolean bigEndian = false;
return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed,
bigEndian);
}
static void listeFormat() {
for (AudioFileFormat.Type t : AudioSystem.getAudioFileTypes()) {
System.out.println(t);
}
}
public JavaSound() throws LineUnavailableException, IOException,
UnsupportedAudioFileException {
Mixer mixer = null;
final JProgressBar progressBar = new JProgressBar();
// la listes des formats ::
listeFormat();
final File input;
final AudioFormat format;
final AudioInputStream audio;
final SourceDataLine line;
input = new File("trip.mid");
audio = AudioSystem.getAudioInputStream(input);
format = audio.getFormat();
// chercher le mixer logiciel (Java Sound Audio Engine) ::
for (Mixer.Info mixerInfo : AudioSystem.getMixerInfo()) {
if (mixerInfo.getName().compareTo("Java Sound Audio Engine") == 0) {
mixer = AudioSystem.getMixer(mixerInfo);
break;
}
}
// la lecture ::
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
line = (SourceDataLine) AudioSystem.getLine(info);
// line = (SourceDataLine) mixer.getLine(info);
// Mixer.Info mixerInfo = AudioSystem.getMixerInfo();
// mixer = AudioSystem.getMixer(mixerInfo);
Runnable runner = new Runnable() {
int BufferSize = (int) format.getFrameRate()
* format.getFrameSize();
byte buffer[] = new byte[BufferSize];
long size = input.length();
long lectureFichier = 0;
Long pos = new Long(0);
@Override
public void run() {
// TODO Auto-generated method stub
int count;
try {
// tout d'abord faut ouvrir la ligne et de lire le flux ::
line.open(format);
line.start();
while ((count = audio.read(buffer, 0, buffer.length)) != -1) {
if (count > -1) {
line.write(buffer, 0, count);
// Timer & ProgressBar traitements ::
lectureFichier = lectureFichier + BufferSize;
pos = new Long((lectureFichier * 1000) / size);
progressBar.setValue(pos.intValue());
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
line.drain();
line.close();
// System.gc();
}
};
// Lecture du son ::
final Thread playThread = new Thread(runner);
// playThread.start();
final JButton btnLire = new JButton("Lire");
final JButton btnRelire = new JButton("Relire");
final JButton btnPause = new JButton("Pause");
final JButton btnStop = new JButton("Stop");
final JCheckBox chckbxMute = new JCheckBox("Mute");
btnLire.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// la lecture du fichier son ::
playThread.start();
line.addLineListener(new LineListener() {
@Override
public void update(LineEvent arg0) {
// TODO Auto-generated method stub
if (arg0.getType() == LineEvent.Type.STOP
|| arg0.getType() == LineEvent.Type.CLOSE) {
btnLire.setEnabled(true);
btnStop.setEnabled(true);
}
}
});
}
});
btnPause.addActionListener(new ActionListener() {
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
// mettre en pose le sound ::
playThread.suspend();
}
});
btnRelire.addActionListener(new ActionListener() {
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
// continuer de lire le sound ::
playThread.resume();
}
});
btnStop.addActionListener(new ActionListener() {
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
// stop le sound ::
playThread.stop();
}
});
chckbxMute.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (line.isControlSupported(BooleanControl.Type.MUTE)) {
mute = (BooleanControl) line
.getControl(BooleanControl.Type.MUTE);
mute.setValue(!mute.getValue());
}
}
});
JPanel contentPane;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 497, 156);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
btnLire.setBounds(41, 65, 72, 23);
contentPane.add(btnLire);
btnPause.setBounds(123, 65, 72, 23);
contentPane.add(btnPause);
btnRelire.setBounds(205, 65, 72, 23);
contentPane.add(btnRelire);
btnStop.setBounds(287, 65, 72, 23);
contentPane.add(btnStop);
progressBar.setBounds(41, 30, 386, 14);
contentPane.add(progressBar);
chckbxMute.setBounds(378, 65, 80, 23);
contentPane.add(chckbxMute);
}
} |