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
|
package MyPocket;
import java.io.*;
import java.security.SecurityPermission;
import javax.sound.sampled.*;
import javax.sound.sampled.Control.Type;
import javax.sound.sampled.Line.Info;
class NewRecorder implements Runnable {
AudioInputStream audioInputStream;
File file;
String fileName ="totot";
TargetDataLine line;
Thread thread;
AudioFormat format;
public NewRecorder(int qualite)
{
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, qualite, 16, 1, 2, qualite, false);
}
public void start()
{
thread= new Thread (this);
thread.start();
}
public void stop()
{
thread = null;
}
public void saveToFile(String name) {
AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;
if (audioInputStream == null) {
return;
} else if (file != null) {
createAudioInputStream(file, false);
}
try {
audioInputStream.reset();
} catch (Exception e) {
return;
}
File file = new File(fileName = name);
try {
if (AudioSystem.write(audioInputStream, fileType, file) == -1) {
throw new IOException("Problems writing to file");
}
} catch (Exception ex) { ex.toString(); }
}
public void createAudioInputStream(File file, boolean updateComponents) {
if (file != null && file.isFile())
{
try {
this.file = file;
audioInputStream = AudioSystem.getAudioInputStream(file);
fileName = file.getName();
} catch (Exception ex) {ex.printStackTrace();}
}
}
public void run() {
audioInputStream = null;
DataLine.Info info = new DataLine.Info(TargetDataLine.class,format);
if(!AudioSystem.isLineSupported(info)) {
System.out.println("Line Matching "+ info+" not supported");
return;
}
try {
line = (TargetDataLine)AudioSystem.getLine(info);
line.open(format,line.getBufferSize());
}catch(LineUnavailableException ex) { System.out.println(ex); return;} catch (Exception ex) { System.out.println(ex); return; }
ByteArrayOutputStream out = new ByteArrayOutputStream();
int frameSizeInBytes = format.getFrameSize();
int bufferLengthInFrames = line.getBufferSize() / 8;
int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
byte[] data = new byte[bufferLengthInBytes];
int numBytesRead;
line.start();
while (thread != null) {
if((numBytesRead = line.read(data, 0, bufferLengthInBytes)) == -1) {
break;
}
out.write(data, 0, numBytesRead);
}
line.stop();
line.close();
line = null;
try {
out.flush();
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
byte audioBytes[] = out.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(audioBytes);
audioInputStream = new AudioInputStream(bais, format, audioBytes.length / frameSizeInBytes);
try {
audioInputStream.reset();
} catch (Exception ex) {
ex.printStackTrace();
return;
}
}
} |
Partager