Question

The title basically tells my story, I would like to send audio directly to the speaker system from a java program, i was thinking of trying to write directly to the speakers in /dev/ in linux using bytes obtained from an audio file, which i'm not sure would work, but in Windows i'm beyond stumped. I was thinking along the lines of something like this:

public void play(byte[] audio){
   if(OS.isWindows){//This is where i have no idea, maybe write to javasound with jmf?
}
if(OS.isLinux){
    BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream("/dev/blahblah");
    bout.write(audio)
    bout.flush();
    bout.close();
 }

My code almost seems to simple to me, not that i want complicated code, but i want working code. I havent actually tried the above code, in fear that i dont know what im doing, and could possibly hurt or interrupt something in my pc.

Was it helpful?

Solution

You can't just cat tune.wav > /dev/dsp anymore (not without some tricks), and the way your attempting this sounds too low level - so choose a library.

You might have luck with the standard ones such as javax.sound (simple) and JMF (more features).

Or you could try a third party library such as Jogamp JOAL (feature rich), which you can use it with Windows and Linux (through native libraries).

EDIT: For MP3

  1. Just noticed with Java 7's javafx you can do easily, see this answer

  2. To roll your own with javax.sound: follow this tutorial on creating for SPI* to create an AudioFileReader handling MP3s. (Note the previous SPI link is javax.sound specific, but SPI is a general concept)

  3. Alternatively JMF can handle mp3s with this, and you can lookup how to nest jars etc if you're determined to provide a single artifact.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top