Re: Java Autio
Finally solved my problem
I uploaded my audio product under the following web site:
http://www.freimann.eu/eliphi/phi/setzkasten/
The audio - version lies under:
http://www.freimann.eu/eliphi/phi/setzkasten/Setzkasten_sound.jar
Thanks again for all your help
PS The code I finally used is the following:
import java.io.*;
import javax.sound.sampled.*;
import eu.gressly.io.ResourceAnchor;
public class Play {
public static void playSampleFile(String name, float pan, float
gain) throws Exception{
InputStream wavInputStream = getWavInputStream(name);
if(null != wavInputStream) {
playSampleFile(wavInputStream, pan, gain);
}
}
public static void playSampleFile(String name) throws Exception {
playSampleFile(name, 0.0f, 0.0f);
}
/**
* Openes a resource as stream. Adds the directory "/wav/" and the
suffix ".wav" to the given wavName
* TODO: cache input Strams in memory (Hashtable using names as keys and
inputStreams as values.
* if the value is there: reset the stream.
* @param wavName Filename (as resource) without "wav/" and without ".wav"
* @return null or a correctly opened inputStream.
*/
private static InputStream getWavInputStream(String wavName) {
ClassLoader cl = ResourceAnchor.class.getClassLoader();
String resourceName = "wav/" + wavName.toLowerCase() + ".wav";
return cl.getResourceAsStream(resourceName);
}
private static void playSampleFile(InputStream wavInputStream, float
pan, float gain)
throws Exception {
AudioInputStream ais =
AudioSystem.getAudioInputStream(wavInputStream);
AudioFormat format = ais.getFormat();
// ALAW/ULAW samples in PCM konvertieren
if ((format.getEncoding() == AudioFormat.Encoding.ULAW)
|| (format.getEncoding() == AudioFormat.Encoding.ALAW)) {
AudioFormat tmp = new
AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
format.getSampleRate(), format.getSampleSizeInBits()
* 2,
format.getChannels(), format.getFrameSize() * 2, format
.getFrameRate(), true);
ais = AudioSystem.getAudioInputStream(tmp, ais);
format = tmp;
}
// Clip erzeugen und fffffffnen
DataLine.Info info = new DataLine.Info(Clip.class, format,
((int) ais
.getFrameLength() * format.getFrameSize()));
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(ais);
// PAN einstellen
FloatControl panControl = (FloatControl) clip
.getControl(FloatControl.Type.PAN);
panControl.setValue(pan);
// MASTER_GAIN einstellen
FloatControl gainControl = (FloatControl) clip
.getControl(FloatControl.Type.MASTER_GAIN);
gainControl.setValue(gain);
// Clip abspielen
clip.start();
while (true) {
try {
Thread.sleep(300);
} catch (Exception e) {
// nothing
}
if (!clip.isRunning()) {
try {
Thread.sleep(200);
} catch (InterruptedException iux) {
}
break;
}
}
clip.stop();
clip.close();
}
public static void main(String[] args) {
try {
playSampleFile(args[0], Float.parseFloat(args[1]), Float
.parseFloat(args[2]));
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
System.exit(0);
}
}
phi wrote:
Hi all
Is this the right place for Java-Audio Questions?
If so: How do I make java to play an Audio File (eg. WAV).
My Application uses Swing and a Key-Event should play a sound.
Either of the following answers should do:
a) Minimal code Example
b) Link to minimal examples (I have found something on
http://www.jsresources.org/examples/SimpleAudioPlayer.java.html but it
seams rather big for just playing a wave file).
Greetings and thanks in advance