简体   繁体   中英

How do I tell if the end user has a sound card in Java?

I think the title says it all. My Java program uses sound and I would like to do a check at startup rather than catch an exception. If it helps I am using FreeTTS - https://freetts.sourceforge.io/ .

You can query the java AudioSystem class for available lines:

/**
 * Gets a list of all audio output devices in the system
 */
public static List<Mixer> getAvailableAudioOutputDevices() {
    final ArrayList<Mixer> available = new ArrayList<>();
    final Mixer.Info[] devices = AudioSystem.getMixerInfo();
    final Line.Info sourceInfo = new Line.Info(SourceDataLine.class);
    for (int i=0; i<devices.length; ++i) {
        final Mixer.Info mixerInfo = devices[i];
        final Mixer mixer = AudioSystem.getMixer(mixerInfo);
        if (mixer.isLineSupported(sourceInfo)) {
            // the device supports output, add as suitable
            available.add(mixer);
        }
    }
    return available;
}

Do note that this does not expliclity identify sound cards, but devices that provide audio output. Thats not necessarily the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM