如何在 Java 中播放提示音以外的声音?
How to play a sound other than beep in Java?
所以目前,这是 Java 中的 "beep"(在某些 OS 上并不是真正的哔哔声):
Toolkit.getDefaultToolkit().beep();
但是代码使用了我电脑的"beep"/提示音...
有没有办法替换它?
我发现了一个 interesting code here,它使用不同于嘟嘟声的声音:
import javax.sound.sampled.*;
public class SoundUtils {
public static float SAMPLE_RATE = 8000f;
public static void tone(int hz, int msecs)
throws LineUnavailableException
{
tone(hz, msecs, 1.0);
}
public static void tone(int hz, int msecs, double vol)
throws LineUnavailableException
{
byte[] buf = new byte[1];
AudioFormat af =
new AudioFormat(
SAMPLE_RATE, // sampleRate
8, // sampleSizeInBits
1, // channels
true, // signed
false); // bigEndian
SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
sdl.open(af);
sdl.start();
for (int i=0; i < msecs*8; i++) {
double angle = i / (SAMPLE_RATE / hz) * 2.0 * Math.PI;
buf[0] = (byte)(Math.sin(angle) * 127.0 * vol);
sdl.write(buf,0,1);
}
sdl.drain();
sdl.stop();
sdl.close();
}
public static void main(String[] args) throws Exception {
SoundUtils.tone(1000,100);
Thread.sleep(1000);
SoundUtils.tone(100,1000);
Thread.sleep(1000);
SoundUtils.tone(5000,100);
Thread.sleep(1000);
SoundUtils.tone(400,500);
Thread.sleep(1000);
SoundUtils.tone(400,500, 0.2);
}
}
只需创建一个这样的函数,然后随心所欲地调用它,例如单击按钮或侦听器。
public void alert() {
try{
InputStream inputStream = getClass().getResourceAsStream("/sounds/sms.wav"); //Note this is path to whatever wav file you want
AudioStream audioStream = new AudioStream(inputStream);
AudioPlayer.player.start(audioStream);
}
catch (IOException e) {
// Whatever exception you please;
}
// return inputStream;
}
另请注意:Audiostream Class 是 Java 专有的,如果它是一个冗长的声音字节并且需要随时停止它,您可以在任何地方调用 AudioPlayer.player.stop(audioStream)使用定时器、线程中断或按钮点击。
所以目前,这是 Java 中的 "beep"(在某些 OS 上并不是真正的哔哔声):
Toolkit.getDefaultToolkit().beep();
但是代码使用了我电脑的"beep"/提示音...
有没有办法替换它?
我发现了一个 interesting code here,它使用不同于嘟嘟声的声音:
import javax.sound.sampled.*;
public class SoundUtils {
public static float SAMPLE_RATE = 8000f;
public static void tone(int hz, int msecs)
throws LineUnavailableException
{
tone(hz, msecs, 1.0);
}
public static void tone(int hz, int msecs, double vol)
throws LineUnavailableException
{
byte[] buf = new byte[1];
AudioFormat af =
new AudioFormat(
SAMPLE_RATE, // sampleRate
8, // sampleSizeInBits
1, // channels
true, // signed
false); // bigEndian
SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
sdl.open(af);
sdl.start();
for (int i=0; i < msecs*8; i++) {
double angle = i / (SAMPLE_RATE / hz) * 2.0 * Math.PI;
buf[0] = (byte)(Math.sin(angle) * 127.0 * vol);
sdl.write(buf,0,1);
}
sdl.drain();
sdl.stop();
sdl.close();
}
public static void main(String[] args) throws Exception {
SoundUtils.tone(1000,100);
Thread.sleep(1000);
SoundUtils.tone(100,1000);
Thread.sleep(1000);
SoundUtils.tone(5000,100);
Thread.sleep(1000);
SoundUtils.tone(400,500);
Thread.sleep(1000);
SoundUtils.tone(400,500, 0.2);
}
}
只需创建一个这样的函数,然后随心所欲地调用它,例如单击按钮或侦听器。
public void alert() {
try{
InputStream inputStream = getClass().getResourceAsStream("/sounds/sms.wav"); //Note this is path to whatever wav file you want
AudioStream audioStream = new AudioStream(inputStream);
AudioPlayer.player.start(audioStream);
}
catch (IOException e) {
// Whatever exception you please;
}
// return inputStream;
}
另请注意:Audiostream Class 是 Java 专有的,如果它是一个冗长的声音字节并且需要随时停止它,您可以在任何地方调用 AudioPlayer.player.stop(audioStream)使用定时器、线程中断或按钮点击。