-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSonido.java
More file actions
56 lines (49 loc) · 1.5 KB
/
Sonido.java
File metadata and controls
56 lines (49 loc) · 1.5 KB
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
import java.io.*;
import javax.sound.sampled.*;
public class Sonido {
// Definimos las variables necesarias para reproducir sonido
private AudioFormat audioFormat;
private AudioInputStream audioInputStream;
private SourceDataLine sourceDataLine;
public Sonido(String path) {
try{
// Creamos y cargamos el archivo de sonido
File soundFile = new File(path);
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
audioFormat = audioInputStream.getFormat();
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class,audioFormat);
sourceDataLine = (SourceDataLine)AudioSystem.getLine(dataLineInfo);
// Iniciamos la reproduccion
new PlayThread().start();
}catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
// Hebra con la que reproduciremos el sonido
class PlayThread extends Thread{
byte tempBuffer[] = new byte[10000];
public void run(){
try{
// Abrimos el archivos y comenzamos el flujo de datos
sourceDataLine.open(audioFormat);
sourceDataLine.start();
int cnt;
// Leemos los datos
while((cnt = audioInputStream.read(
tempBuffer,0,tempBuffer.length)) != -1){
if(cnt > 0){
// Escribimos los datos en la linea de sonido
sourceDataLine.write(tempBuffer, 0, cnt);
}
}
sourceDataLine.drain();
// Cerramos el flujo
sourceDataLine.close();
}catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
}
}