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
|
/**
* File AudioFileReader.java
* Created on 30 mai 2008
*/
package util;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
/**
*
* @author Absil Romain.
*/
public class AudioFileReader
{
public AudioFileReader()
{
}
public static void readAudioFile(File file)
throws UnsupportedAudioFileException, IOException,
LineUnavailableException
{
AudioInputStream in = AudioSystem.getAudioInputStream(file);
AudioInputStream din = null;
AudioFormat baseFormat = in.getFormat();
AudioFormat decodedFormat =
new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
baseFormat.getSampleRate(),
16,
baseFormat.getChannels(),
baseFormat.getChannels() * 2,
baseFormat.getSampleRate(),
false);
din = AudioSystem.getAudioInputStream(decodedFormat, in);
rawplay(decodedFormat, din);
in.close();
}
public static void rawplay(AudioFormat targetFormat, AudioInputStream din)
throws IOException, LineUnavailableException
{
byte[] data = new byte[4096];
SourceDataLine line = getLine(targetFormat);
if (line != null)
{
// Start
line.start();
int nBytesRead = 0, nBytesWritten = 0;
while (nBytesRead != -1)
{
nBytesRead = din.read(data, 0, data.length);
if (nBytesRead != -1)
nBytesWritten = line.write(data, 0, nBytesRead);
}
for(int i=0;i<29;i++)
{
System.out.print( data[i]+" ");
}
// Stop
line.drain();
line.stop();
line.close();
din.close();
}
}
public static SourceDataLine getLine(AudioFormat audioFormat)
throws LineUnavailableException
{
SourceDataLine res = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
res = (SourceDataLine) AudioSystem.getLine(info);
res.open(audioFormat);
return res;
}
public static void main (String[]args)
{
try {
AudioFileReader pp = new AudioFileReader();
pp.readAudioFile(new File("C:\\Documents and Settings\\Zolekha\\Bureau\\alarme.wav"));
pp.rawplay(null, null);
Thread.sleep(100); // fait patienter 0.1 seconde
// int i = 0;
/** while(i <= 10){
Thread.sleep(1000); // fait patienter une seconde
System.out.println(i);
i++;
}*/
System.out.println("\ndebug0");
}
catch (Exception e)
{
e.printStackTrace();
}
}
} |
Partager