7 Replies Latest reply on Aug 13, 2010 9:22 PM by captfoss

    Substitute of java.awt.ToolKit.getDefaultToolKit.beep()

      Hello everybody. I am designing a Swing Application and there I am using java.awt.ToolKit.getDefaultToolKit.beep() method to generate sound in various operations. I want to know is it possible to generate own sound as a substitute of this method? I think it is possible. How can I do this? Thank you.
        • 1. Re: Substitute of java.awt.ToolKit.getDefaultToolKit.beep()
          Not entirely sure what you're trying to achieve here.

          Are you trying to exchange the sound played when beep() is invoked? If so, the answer is no, you can't change it from java, it is a system default.

          Are you asking if you can play custom sounds from java? Sure you can, just check out http://www.jsresources.org/ for an overview of what can be done in pure java.
          • 2. Re: Substitute of java.awt.ToolKit.getDefaultToolKit.beep()
            Thank you for giving reply.
            As I wish to play .mp3 file I had downloaded an API from Javalobby . I tried it, here is my code :
            package player;
            import java.io.File;
            import java.io.IOException;
            import javax.sound.sampled.AudioFormat;
            import javax.sound.sampled.AudioInputStream;
            import javax.sound.sampled.AudioSystem;
            import javax.sound.sampled.DataLine;
            import javax.sound.sampled.SourceDataLine;
             * @author Tanmoy
             * @Super Scientific Calculator
             * @version 1.2010
            public class Player {
                 private File audioFile;
                 public Player(String path) {
                      this.audioFile = new File(path);
                 public void play() {
                      AudioInputStream din = null;
                      try {
                           AudioInputStream in = AudioSystem.getAudioInputStream(audioFile);
                           AudioFormat baseFormat = in.getFormat();
                           AudioFormat decodedFormat = new AudioFormat(
                                baseFormat.getSampleRate(), 16, baseFormat.getChannels(),
                                baseFormat.getChannels() * 2, baseFormat.getSampleRate(),
                           din = AudioSystem.getAudioInputStream(decodedFormat, in);
                           DataLine.Info info = new DataLine.Info(SourceDataLine.class, decodedFormat);
                           SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
                           if (line != null) {
                                byte[] data = new byte[4096];
                                int nBytesRead;
                                while ((nBytesRead = din.read(data, 0, data.length)) != -1) {
                                     line.write(data, 0, nBytesRead);
                      } catch (Exception e) {
                      } finally {
                           if (din != null) {
                                try {
                                } catch (IOException e) {
            And the calling, piece of code is :
            try {
                                          Parser parser = new Parser(parsingString, displayPanel.getCurrentBase(),
                                               displayPanel.getCurrentBase(), displayPanel.getTrigoMode(),
                                          String output = parser.solve();
                                          if (message == null) {
                                               message = "No Exception";
                                     } catch (InvalidSyntaxException invalidSyntaxException) {
                                          message = invalidSyntaxException.getMessage();
                                          JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);
                                          new Player("/resources/what_the_hell.mp3").play();
            But I can not able to here the sound.
            What to do now?
            Thank you.
            • 3. Re: Substitute of java.awt.ToolKit.getDefaultToolKit.beep()
              I have figured here is the problem,
              Calling code :
              // I am trying to pass URI, as File("/resources/what_the_hell.mp3") is not working
              new Player("file://C:/Users/Tanmoy/Documents/NetBeansProjects/Super Scientific Calculator/src/resources/what_the_hell.mp3").play();
              The Player(String path) constructor is :
              public Player(String path) {
                        URI uri = null;
                        try {
                             uri = new URI(path);
                        } catch (URISyntaxException ex) {
                             Logger.getLogger(Player.class.getName()).log(Level.SEVERE, null, ex);
                        if (uri == null) {
                                      // debug purpose and it prints error
                        } else {
                             this.audioFile = new File(uri);
              And I have omitted the
              JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);
              I put the .mp3 file in /project folder/src/resources. I can not able to understand why it is happening. Any suggestion will be very helpful to me. I always got this File access problem!
              • 4. This Thread is now moved
                Note: This thread was originally posted in the Swing forum, but moved to this forum for closer topic alignment.
                • 5. Re: Substitute of java.awt.ToolKit.getDefaultToolKit.beep()
                  JavaSound doesn't support MP3 file without installing a special plugin...

                  • 6. Re: Substitute of java.awt.ToolKit.getDefaultToolKit.beep()
                    I had installed it. I placed it in /lib/ext along with jl1.0.jar and tritonus_share.jar.
                    The path of URI is :
                    The code :
                    public Player(String path) {
                              URI uri = null;
                              try {
                                   uri = new URI("file://C:/Users/Tanmoy/Documents/NetBeansProjects/Super_Scientific_Calculator/src/resources/what_the_hell.mp3");
                              } catch (URISyntaxException ex) {
                                   Logger.getLogger(Player.class.getName()).log(Level.SEVERE, null, ex);
                              if (uri != null) {
                                   this.audioFile = new File(uri);
                    Giving this run time
                    Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: URI has an authority component
                            at java.io.File.<init>(File.java:368)
                            at player.Player.<init>(Player.java:40)
                            at mainpackage.OperatorPanel$2.actionPerformed(OperatorPanel.java:195)
                            at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
                            at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
                            at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
                            at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
                            at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
                            at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:272)
                            at java.awt.Component.processMouseEvent(Component.java:6267)
                            at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
                            at java.awt.Component.processEvent(Component.java:6032)
                            at java.awt.Container.processEvent(Container.java:2041)
                            at java.awt.Component.dispatchEventImpl(Component.java:4630)
                            at java.awt.Container.dispatchEventImpl(Container.java:2099)
                            at java.awt.Component.dispatchEvent(Component.java:4460)
                            at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
                            at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
                            at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
                            at java.awt.Container.dispatchEventImpl(Container.java:2085)
                            at java.awt.Window.dispatchEventImpl(Window.java:2478)
                            at java.awt.Component.dispatchEvent(Component.java:4460)
                            at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
                            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
                            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
                            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
                            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
                            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
                            at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
                    In this case URI not returning null. What to do?
                    • 7. Re: Substitute of java.awt.ToolKit.getDefaultToolKit.beep()
                      In this case URI not returning null. What to do?
                      Stop using a URI and just use the string to construct the file object...