I'm trying to work with sound in Java and to work with a program I wrote a while ago. This program uses the AudioFormat to know the sound format. But since JDK-9, AudioFormat doesn't exist anymore. The import:
import javax.sound.sampled.AudioFormat;
gives an error:
the import javax.sound.sampled can not be resolved.
What idea did Oracle have to replace javax.sound?
Edit: Here is the code of a simple sound file reader which is ending with the above error:
package sound.sourcing;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import commonTools.model.Buffer;
import commonTools.model.SoundPacket;
public class SoundFileReader {
private File file;
private AudioInputStream ais = null;
private AudioFormat audioFormat;
private SoundPacket soundPacket;
private Buffer buffer;
private Reader reader;
private int generationPeriod = 100;
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
public SoundFileReader(File file) {
this.file=file;
buffer=new Buffer(20);
initialize();
}
private void initialize() {
try {
ais = AudioSystem.getAudioInputStream(file);
} catch (UnsupportedAudioFileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
audioFormat = ais.getFormat();
/*
* On calcule la duree totale du morceau
* en divisant le nombre total de frames par le frame rate par secondes
*/
//long frames = ais.getFrameLength();
reader=new Reader();
/*
* À chaque schedule, on génère une durée de signal = 1/fréquence = 1 période
* (voir calcul de periodLengthInFrames dans le constructeur)
* Le scheduler doit donc être cadencé sur cette période
*/
scheduler.scheduleAtFixedRate(reader, 0, generationPeriod, TimeUnit.MILLISECONDS);
}
private class Reader implements Runnable {
public Reader () {
}
@Override
public synchronized void run() {
int packetId=0;
byte bytes[] = new byte[2048];
int bytesRead=0;
try {
while ((bytesRead=ais.read(bytes, 0, bytes.length)) !=-1) {
soundPacket=new SoundPacket(System.currentTimeMillis(), packetId, 2048, audioFormat, bytes);
buffer.loadWithReconstruction(soundPacket);
//System.out.println("AudioFileReader.read : buffer loaded with packet "+packetId);
packetId += 1;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//Getters and Setters
public Buffer getBuffer() {
return buffer;
}
}
Comments
Post a Comment