Plug and Unplug Headphones

Hi everyone,

In my game, it is necessary, to unplug the headphones of the PC and plug in the speakers, while the game is running. I know, it sounds like a weird request, but its part of a weird scientific experiment :slight_smile: I guess, that unplugging the headphones does completely deactivate the audio renderer, since even after plugging them in again, there is no sound rendered at all.

Is there a way to listen to a “plug” event of the audio jack in jMonkey or in Java and to re-activate the audio renderer or what ever causes the mute?

Thanks for any help,

P.

This is not an easy solution…

I’m not sure if there’s any specific event for detecting when audio devices are connected in Java, but I do know you can use the AudioSystem class in javax to find audio devices.

http://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/class-use/Mixer.Info.html

You might be able to use AudioSystem.isLineSupported() to check for a headphone port.

For example:

[java]

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.Port.Info;

/**
*

  • @author Navid
    */
    public class TestAudioSystem {

    public static void main(String[] args) {

     for (Mixer.Info info : AudioSystem.getMixerInfo()) {
         System.out.println(info.getName() + ", " + info.getVersion() + ", " + info.getVendor() + ", " + info.getDescription());
     }
     
     String available = isHeadsetAvailable() ? "Headphones found!" : "Headphones not found!";
     System.out.println(available);
    

    }

    public static boolean isHeadsetAvailable() {
    return AudioSystem.isLineSupported(Info.HEADPHONE);
    }
    }
    [/java]

Unfortunately, AudioSystem.isLineSupported(Info.HEADPHONE) does not always work, because some computers do not have a specific headphones port/device on their computer, and simply have an audio-out jack. You will probably have to do some additional checks. You can always simply ask the user if they have headphones plugged in with a GUI prompt or something.

That’s all I know on this, sadly, someone else can probably tell you more.

EDIT: I just read more of the post, silly me, it seems like a completely different issue, unplugging the headphones should not cause the sound to turn off completely… that’s odd.

On top of the fact that you don’t really have a way to detect this, not all audio interfaces actually do anything when you plug a headphone. Many laptop headphone jacks do but not even its all of them.

Thanks @normen and @navid113 for your replies. I will try to figure out a different way. A work-around that seems to work is rather hardware solution, but probably even better for the otherwise excessivley used jack:

Using an extension I simply unplug the headphones from the extension and plug the speakers into the extension, and now it seems to work just fine …