Table of Contents
Question content
Workaround
Home Java What Java function can I use on macOS to play the appropriate alert sound at the appropriate volume on the appropriate device?

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

Feb 22, 2024 pm 12:40 PM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)