views:

371

answers:

2

I would have thought that this would be an easy thing to do, but no amount of googling around has turned up any solutions.

I have written an application for a client that runs in full screen and allows the user to page through educational books in order to help teach kids to read. So far so good. Some phrases from the displayed materials are read back to the user and, again, so far so good. Normally, these sounds are read at the system volume. (ie, at whatever volume any other system sound would be played.)

The client, however, now wants the user to be able to adjust the volume in program. I have the UI and processing end of that working, but I'm having a hard time adjusting the volume of the clips being played in a meaningful way. Right now I have a funky setup that involves reading the original gain of the clip and then adjusting that up or down for each clip. While this does adjust the volume, it does not seem to do it relative to the system volume -- all of the sounds are much quieter than the system volume.

So, my question really is: how do you suggest controlling sound volume within a program? All of my research has turned up nothing meaningful, which implies to me that it's kind of hard or even impossible, but that just doesn't seem right.

Oh, details: I'm reading in WAVs as AudioInputStreams and playing them as java.sound.sampled.Clip. I'm controlling the gain using FloatControl.Type.MASTER_GAIN. (FloatControl.Type.VOLUME is not supported.) I'm stuck using Java 1.4.

A: 

This is an OS-specific thing to do, and you'll have to use either JNI or J/Invoke or JNIWrapper or jna or...

Jonathan Feinberg
+1  A: 

You might also look to see if there is an older version of the Java Media Framework (JMF) that will support Java 1.4... it might provide you with a more rich environment for working with audio.

Other than that, you say you are stuck in 1.4... you might see if there is any way you can get upgraded to Java 5 or 6, though you may want to test either of these first on your local machine to see if they actually help any.

I guess one last suggestion is to find some Java game development sites and post your question there; I would think they would have figured out ways around it... though you could also get a lot of 'upgrade your jvm' responses there too. :-)

Good luck.

cjstehno
JMF is perfect. Many folks are complaining that's it outdated, and doesn't support modern codecs, but it's perfect for my dated JVM + simple WAV file situation. And it's very easy to adjust the volume.
Clayton