0

I have tried using both

Toolkit.getDefaultToolkit().beep(); and

System.out.println("\007");

and neither are actually playing a sound. I tried running the code in my IDE (CodeRunner 2) and in Terminal to see if it made a difference, which it didn't.

If anyone know another way to do this or why it isn't working, please let me know

Thanks!

  • Do you hear sound at all when you type `tput bel` or [something similar](http://stackoverflow.com/q/3127977/3429133) in your terminal? – beatngu13 Oct 21 '16 at 21:24
  • `Toolkit.getDefaultToolkit().beep();` works for me. Are you sure everything else is as it should be? Did you try beatngu's suggestion (which also played the System beep on my machine)? – 11684 Aug 21 '17 at 18:42

2 Answers2

-1

I think you are looking for Runtime#exec(String):

Runtime.getRuntime().exec(cmd);

This should allow you to get an instance of the current environment in which you can issue such commands.

beatngu13
  • 7,201
  • 6
  • 37
  • 66
a-sir
  • 74
  • 8
  • Thanks you all though this isn't exactly working, could you show some of the formalities? –  Oct 21 '16 at 20:38
  • Been trying it myself right now and can't get the damn thing to beep XD, Toolkit.getDefaultToolkit().beep(); does seem to work for me though – a-sir Oct 21 '16 at 20:43
  • My brother just came to the rescue. getRuntime is a method. You wrote it here without the brackets. –  Oct 21 '16 at 20:56
  • Yeah i'm seeing that myself right now... stupid mistake! Still i do wonder why the Toolkit.getDefaultToolkit().beep(); doesn't work for you – a-sir Oct 21 '16 at 21:00
  • @a-sir Next time: if you're seeing a mistake you made, please fix it by editing your post. – beatngu13 Oct 22 '16 at 08:01
-1

Or you might try to use it with JNA-Library:

   import com.sun.jna.platform.win32.Kernel32;
   import com.sun.jna.Native;
   import com.sun.jna.Library;

   interface JnaTests extends Library {
    public boolean Beep(int FREQUENCY , int DURATION );
    static Kernel32 kernel32 = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class); 
   static void startBeep() throws InterruptedException { 
   kernel32.Beep(1200, (5000)); 
   Thread.sleep(50); 
  }

}


  package com.sun.jna.platform;


  import com.sun.jna.Library;

   public class win32 {

   private static class MSG implements User32 {

    public MSG() {
    }
   }

  public interface Kernel32 extends Library { // ... (lines deleted for   clarity) ...  
    boolean Beep(int frequency, int duration); 
     //int GetLogicalDrives(); // ... (lines deleted for clarity)   ...      }   
    }
   public interface User32 extends Library { // ... (lines deleted for  clarity) ...  
        // ... (lines deleted for clarity) ... }
   }
  }

  Perhaps you could use the midi class too:



Sequencer sequencer = MidiSystem.getSequencer();
sequencer.open();
Sequence sequence = new Sequence(Sequence.PPQ,4);
Track track = sequence.createTrack();
ShortMessage a = new ShortMessage();
a.setMessage(144,9,56,100);
MidiEvent event = new MidiEvent(a, 1);
track.add(event);
sequencer.setSequence(sequence);
sequencer.start();
Thread.sleep(500);
sequencer.close();
loadP
  • 404
  • 1
  • 4
  • 15