Home > Java > body text

What Java function can I use on macOS to play the appropriate alert sound at the appropriate volume on the appropriate device?

王林
Release: 2024-02-22 12:40:20
forward
468 people have browsed it

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.

Question content

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:

  • Alarm sound (sound name)
  • Play sound effects via (device name)
  • Alert volume (slider setting)

what should I do?

Workaround

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();
Copy after login

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(); }
      });
   }
}
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template