php Editor Xiaoxin In the java Q&A, a reader asked a question: "What Java function can I use on macOS to play the appropriate alarm sound at the appropriate volume on the appropriate device?" This question involves Using Java to control the device to play alarm sounds on the macOS system needs to be implemented in conjunction with the Java audio processing library. The specific operation methods and function calls can be solved by in-depth study of relevant Java audio processing knowledge.
I want my Java application to respect the macOS settings in System Settings... when playing alert sounds. › Sound › Sound effects.
For example, whenever someone presses backspace
and the cursor is at the leftmost position in the jTextField, the program will play a user-configured "alert sound".
There is a similar need to play the same alert sound in other functions of my app. But I haven't found a library that allows my app to play the system-appropriate "alert sound" at the appropriate volume on the appropriate device.
I want my app to follow the following macOS system settings:
what should I do?
I have tried Toolkit.beep on macos and it seems to do what you want. I haven't verified that jtextfield calls toolkit.beep, but it seems to be calling it. One can look at the openjdk source code and might get a better idea.
On windows, jtextfield seems to use a default beep. And toolkit.beep.
As long as the default toolkit works (which works for me), you can place the following in your app wherever you need to alert the user.
toolkit tk = toolkit.getdefaulttoolkit(); tk.beep();
This is my mcv example.
import java.awt.event.*; import java.awt.BorderLayout; import java.awt.Toolkit; import javax.swing.*; public class Alert extends JFrame { private Toolkit tk = Toolkit.getDefaultToolkit(); public Alert () { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextField tf = new JTextField(); JButton bbeep = new JButton(new AbstractAction() { public void actionPerformed (ActionEvent e) {tk.beep();} }); setLayout(new BorderLayout()); getContentPane().add(tf, BorderLayout.NORTH); getContentPane().add(bbeep, BorderLayout.SOUTH); pack(); setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new Alert(); } }); } }
The above is the detailed content of What Java function can I use on macOS to play the appropriate alert sound at the appropriate volume on the appropriate device?. For more information, please follow other related articles on the PHP Chinese website!