如何在Mac上播放哔声/警报声在Java中

时间:2016-10-21 19:00:16

标签: java

我尝试过使用

Toolkit.getDefaultToolkit().beep();

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

并且实际上都没有播放声音。我尝试在我的IDE(CodeRunner 2)和终端中运行代码以查看它是否有所不同,但它没有。

如果有人知道另一种方法,或者为什么它不起作用,请告诉我

谢谢!

2 个答案:

答案 0 :(得分:-1)

我认为您正在寻找Runtime#exec(String)

Runtime.getRuntime().exec(cmd);

这应该允许您获取当前环境的实例,您可以在其中发出此类命令。

答案 1 :(得分:-1)

或者您可以尝试将其与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();