Best way to get Sound on Button Press for a Java Calculator?

java play sound on button click
play button in java
make sound in java
adding sound effects to java
how to read and write audio file in java
how to stop audio in java
accessing image and audio files in java
java audio library

I'm a learning Java student working on an independent project for my Resume. I decided to do a Java calculator because I know most of the components that make it up. One thing I don't know how to do is add sound on the button press. I have a vague idea what with the Audiostream input stuff. But I need unique sounds for each button on my calculator. Right now my calculator isn't completely finished (listeners don't work yet.) I was just wondering what would be the best way to incorporate my .wav files into the button presses. Thanks in advance.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculator {

    //instance variables
    JFrame frame;
    JPanel mainPanel, northPanel, southPanel;
    JTextField numberLabel;
    JButton backspace, multiply, divide, add, subtract, equal; //four function buttons
    JButton one, two, three, four, five, six, seven, eight, nine, zero; //number buttons
    JButton posOrNeg, decimal, leftParenthese, rightParenthese;

    //constructor
    public Calculator()
    {
        //create it
        frame = new JFrame();
        mainPanel = new JPanel(); //contains both panels
        mainPanel.setForeground(Color.BLACK);
        mainPanel.setBackground(Color.DARK_GRAY);
        northPanel = new JPanel(new BorderLayout()); //contains the number label in border layout
        southPanel = new JPanel(new GridLayout(5, 4)); //contains the buttons in border layout
        numberLabel = new JTextField(37); //*************************
        backspace = new JButton();
        backspace.setForeground(Color.WHITE);
        backspace.setBackground(Color.GRAY);
        multiply = new JButton();
        multiply.setForeground(Color.WHITE);
        multiply.setBackground(Color.MAGENTA);
        divide = new JButton();
        divide.setForeground(Color.WHITE);
        divide.setBackground(Color.PINK);
        add = new JButton();
        add.setForeground(Color.WHITE);
        add.setBackground(Color.BLUE);
        subtract = new JButton();
        subtract.setForeground(Color.WHITE);
        subtract.setBackground(Color.RED);
        equal = new JButton();
        equal.setForeground(Color.WHITE);
        equal.setBackground(Color.ORANGE);
        zero = new JButton();
        zero.setForeground(Color.DARK_GRAY);
        zero.setBackground(Color.GREEN);
        one = new JButton();
        one.setForeground(Color.DARK_GRAY);
        one.setBackground(Color.GREEN);
        two = new JButton();
        two.setForeground(Color.DARK_GRAY);
        two.setBackground(Color.GREEN);
        three = new JButton();
        three.setForeground(Color.DARK_GRAY);
        three.setBackground(Color.GREEN);
        four = new JButton();
        four.setForeground(Color.DARK_GRAY);
        four.setBackground(Color.GREEN);
        five = new JButton();
        five.setForeground(Color.DARK_GRAY);
        five.setBackground(Color.GREEN);
        six = new JButton();
        six.setForeground(Color.DARK_GRAY);
        six.setBackground(Color.GREEN);
        seven = new JButton();
        seven.setForeground(Color.DARK_GRAY);
        seven.setBackground(Color.GREEN);
        eight = new JButton();
        eight.setForeground(Color.DARK_GRAY);
        eight.setBackground(Color.GREEN);
        nine = new JButton();
        nine.setForeground(Color.DARK_GRAY);
        nine.setBackground(Color.GREEN);
        posOrNeg = new JButton();
        posOrNeg.setForeground(Color.WHITE);
        posOrNeg.setBackground(Color.LIGHT_GRAY);
        decimal = new JButton();
        decimal.setForeground(Color.WHITE);
        decimal.setBackground(Color.CYAN);
        leftParenthese = new JButton();

        rightParenthese = new JButton();

        //configure it
        frame.setTitle("My Calculator");
        frame.setSize(450, 225);
        frame.setLocation(200, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        //buttons
        backspace.setText("Back Space");
        leftParenthese.setText("(");
        rightParenthese.setText(")");
        multiply.setText("x");
        divide.setText("/");
        add.setText("+");
        subtract.setText("-");
        equal.setText("=");
        zero.setText("0");
        one.setText("1");
        two.setText("2");
        three.setText("3");
        four.setText("4");
        five.setText("5");
        six.setText("6");
        seven.setText("7");
        eight.setText("8");
        nine.setText("9");
        posOrNeg.setText("+/-");
        decimal.setText(".");

        //add it
        northPanel.add(numberLabel, BorderLayout.NORTH);
        southPanel.add(backspace);
        southPanel.add(leftParenthese);
        southPanel.add(rightParenthese);
        southPanel.add(multiply);
        southPanel.add(seven);
        southPanel.add(eight);
        southPanel.add(nine);
        southPanel.add(divide);
        southPanel.add(four);
        southPanel.add(five);
        southPanel.add(six);
        southPanel.add(add);
        southPanel.add(one);
        southPanel.add(two);
        southPanel.add(three);
        southPanel.add(subtract);
        southPanel.add(zero);
        southPanel.add(decimal);
        southPanel.add(posOrNeg);
        southPanel.add(equal);
        mainPanel.add(northPanel, BorderLayout.NORTH);
        mainPanel.add(southPanel, BorderLayout.SOUTH);
        frame.add(mainPanel);

        //add listener
        one.addActionListener(new ButtonListener());
        two.addActionListener(new ButtonListener());
        three.addActionListener(new ButtonListener());
        four.addActionListener(new ButtonListener());
        five.addActionListener(new ButtonListener());
        six.addActionListener(new ButtonListener());
        seven.addActionListener(new ButtonListener());
        eight.addActionListener(new ButtonListener());
        nine.addActionListener(new ButtonListener());
        zero.addActionListener(new ButtonListener());
        multiply.addActionListener(new ButtonListener());
        divide.addActionListener(new ButtonListener());
        add.addActionListener(new ButtonListener());
        subtract.addActionListener(new ButtonListener());
        equal.addActionListener(new ButtonListener());
        posOrNeg.addActionListener(new ButtonListener());
        decimal.addActionListener(new ButtonListener());
        backspace.addActionListener(new ButtonListener());
        leftParenthese.addActionListener(new ButtonListener());
        rightParenthese.addActionListener(new ButtonListener());

    }

    //define action listener
    class ButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource() == one)
            {

            }
            if(e.getSource() == two)
            {

            }
            if(e.getSource() == three)
            {

            }
            if(e.getSource() == four)
            {

            }
            if(e.getSource() == five)
            {

            }
            if(e.getSource() == six)
            {

            }
            if(e.getSource() == seven)
            {

            }
            if(e.getSource() == eight)
            {

            }
            if(e.getSource() == nine)
            {

            }
            if(e.getSource() == zero)
            {

            }
            if(e.getSource() == multiply)
            {

            }
            if(e.getSource() == divide)
            {

            }
            if(e.getSource() == add)
            {

            }
            if(e.getSource() == subtract)
            {

            }
            if(e.getSource() == equal)
            {

            }
            if(e.getSource() == posOrNeg)
            {

            }
            if(e.getSource() == decimal)
            {

            }
            if(e.getSource() == backspace)
            {

            }
            if(e.getSource() == leftParenthese)
            {

            }
            if(e.getSource() == rightParenthese)
            {

            }
        }
    }    
}
String soundName = "yourSound.wav";    
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(soundName).getAbsoluteFile());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();

This should help you in achieving what you want.

And yeah, you will require these imports:-

import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

javax.sound.sampled.Clip.start java code examples, How do you add an action to a button in Java? Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

 public static synchronized void playSound(final String url) {
    new Thread(new Runnable() { // the wrapper thread is unnecessary, unless it blocks on the Clip finishing, see comments
      public void run() {
        try {
          Clip clip = AudioSystem.getClip();
          AudioInputStream inputStream = AudioSystem.getAudioInputStream(Main.class.getResourceAsStream("/path/to/sounds/" + url));
          clip.open(inputStream);
          clip.start(); 
        } catch (Exception e) {
          System.err.println(e.getMessage());
        }
      }
    }).start();
  }

check the link http://www.developer.com/java/other/article.php/2173111/Java-Sound-Playing-Back-Audio-Files-using-Java.htm

How to Play Audio in Java Swings, Best way to get Sound on Button Press for a Java Calculator? String soundName = "yourSound.wav"; AudioInputStream audioInputStream = AudioSystem. I'm a learning Java student working on an independent project for my Resume. I decided to do a Java calculator because I know most of the components that make it up. One thing I don't know how to do is add sound on the button press. I have a vague idea what with the Audiostream input stuff. But I need unique sounds for each button on my calculator.

I am a learning student too and just found this solution! It's worked for me and is very simple!

URL soundbyte = new File("yourpath/sound.wav").toURI().toURL();
java.applet.AudioClip clip = java.applet.Applet.newAudioClip(soundbyte);
clip.play();

It know it works with .wav files and may also work for .au files... does not seem to work with mp3s.

How to Create Off Button Calculator in Java #5, To get motivated, inspired and take your personal development to next level, Click here to Duration: 5:16 Posted: Sep 16, 2017 Button one = (Button) this.findViewById(R.id.button1); First is the initialization of the button to be used in playing the sound. We use the Activity's findViewById, passing the Id we assigned to it (in this example's case: R.id.button1), to get the button that we need.

Java Swing GUI - Make a Calculator (part 2), off button, off button sound effect, off button not working on iphone, off button iphone x, off Duration: 2:38 Posted: May 21, 2018 Java Code The purpose of this Java program is to show how to implement an ActionListener interface for handling JButton button event clicks by using the containing class, an inner class, and an anonymous inner class.

Add Sound Play on Android Button Click, Java Swing GUI – Make a Calculator (part 2) – Handle button event with In the last article, we've already known how to use JFrame, JPanel Note that we declare the button1 and textField1 in the top so that we implement on a class to make a handler class that listens to the click White noise player. #097 Create Full Calculator Or Generate Full Calculator In JAVA NetBeans تعلم الجافا من البداية الي - Duration: 37:57. Hassouna Academy 93,221 views 37:57

Java for complete beginners - events, How to add sound play effect on button click in Android Application. Here are the Go to MainActivity.java. text and give an id to the button. 3. Hi Thank you for reading my post How i can play sound file in Java? I need to play a wav file when user press a button , is it possible ? can you pleas explain me ?

Comments
  • See Playing Back Audio.
  • How about stackoverflow.com/questions/2416935/… stackoverflow.com/questions/26305/how-can-i-play-sound-in-java ?
  • There is a working SSCCE in the Java Sound info. page. BTW - No need to dump 250 lines of code for this one.
  • .wav is CD quality if I'm not mistaken. Isn't that a bit of an overkill for a mere button sound?
  • @11684 You are mistaken. WAV can be a range of bit depths, mono or stereo & various sampling rates.
  • The thread is not necessary. See this answer for the proof.
  • Should you want to stop the sound, note that "The drain() method blocks until this internal buffer becomes empty," as cited here.