Bonjour,
En Java standard tu as :
Toolkit.getDefaultToolkit().beep(); // import java.awt.Toolkit;
Sinon en fouillant un peu dans le Toolkit on peut récupérer les desktopProperties (spécifiques au système d'exploitation) qui peuvent contenir des sons.
import java.awt.Toolkit;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
@SuppressWarnings("unchecked")
public static <E> Map<String, E> getSpecificDesktopProperties(String initializer, Class<E> clazz) throws Exception {
Toolkit tk = Toolkit.getDefaultToolkit();
tk.getDesktopProperty(initializer);
Field fd = Toolkit.class.getDeclaredField("desktopProperties");
fd.setAccessible(true);
Map<String, Object> desktopProperties = (Map<String, Object>) fd.get(tk);
Map<String, E> specific = new TreeMap<>();
for (Entry<String, Object> entry : desktopProperties.entrySet()) {
Object value = entry.getValue();
if (value != null && clazz.isAssignableFrom(value.getClass()))
specific.put(entry.getKey(), (E) value);
}
return specific;
}
Exemple pour lister les sons Windows et les écouter :
public static void main(String[] args) throws Exception {
for (Entry<String, Runnable> e : getSpecificDesktopProperties("win.sound", Runnable.class).entrySet()) {
System.out.println(e.getKey());
e.getValue().run();
Thread.sleep(1000);
}
}
Et une fois que tu as trouvé le son que tu veux :
((Runnable) Toolkit.getDefaultToolkit().getDesktopProperty("win.sound.default")).run();
La confiance n'exclut pas le contrôle