0

i would like to add sound in my java calculator when an error occurs(e.g when operation *and / are pressed).please help me with the sound code.

3
Contributors
2
Replies
3
Views
12 Years
Discussion Span
Last Post by server_crash
0

You can always play a sound from a file like this:

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

public class Sound extends JFrame implements ActionListener {
		AudioClip song;
   public Sound() {
	super("Sounds");
	setSize(200,130);
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
	Container pane = getContentPane();
	FlowLayout flo = new FlowLayout();
	pane.setLayout(flo);
	JButton btn = new JButton("Press");
	pane.add(btn);
	btn.addActionListener(this);
        setContentPane(pane);
	setVisible(true);
   }

   public void actionPerformed(ActionEvent event) {


	
   String source = event.getActionCommand();
		 	
	if (source == "Press") {
		try { 
			URL songURL = new URL("file:  errorSound.wav");                
			song = JApplet.newAudioClip(songURL);
			song.play();
		} catch (MalformedURLException malform) {
				System.out.println(malform.toString());
                 }
	}
        else 
	{
	 }
   }
   
   public static void main(String[] arguments) {
	Sound sn = new Sound();
   }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.