如何在 Java 应用程序中播放声音(警报)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3780406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to play a sound (alert) in a java application?
提问by Krithika
I am working with a SMS handling, java based software and want to play a beep / alert sound whenever we receive a message. I tried looking at the java.sound libraries and could not find anything. I do not know if going the applet way of playing a sound will be okay in a java application! Are there any predefined sounds in any java libraries which we can call in an application? Any pointers will be appreciated!
我正在使用 SMS 处理、基于 Java 的软件,并且想要在我们收到消息时播放哔声/警报声。我尝试查看 java.sound 库,但找不到任何内容。我不知道在java应用程序中使用applet方式播放声音是否可以!我们可以在应用程序中调用的任何 Java 库中是否有任何预定义的声音?任何指针将不胜感激!
回答by npinti
回答by Adamski
The applet route should be fine (and is very straightforward). To avoid creating an Applet instance you can use the static newAudioClip
method, and then call play()
on the AudioClip
created.
小程序路由应该没问题(并且非常简单)。为避免创建 Applet 实例,您可以使用静态newAudioClip
方法,然后调用play()
已AudioClip
创建的。
URL url = getClass().getResource("/foo/bar/sound.wav");
AudioClip clip = Applet.newAudioClip(url);
clip.play();
Here, the sound.wav
file is bundled in the jar
file in the foo/bar
package that you create. A fully functional class (where the wav
file is in the sounds
package) would look like this:
在这里,sound.wav
文件捆绑在您创建jar
的foo/bar
包中的文件中。一个功能齐全的类(wav
文件在sounds
包中的位置)看起来像这样:
package sounds;
import java.applet.Applet;
import java.applet.AudioClip;
public class PlaySound {
public void PlayBeep() {
AudioClip clip = Applet.newAudioClip(getClass().getResource("/sounds/beep3.wav"));
clip.play();
}
}
Here, the path is given as /sounds/
because when you extract the jar
, you'll see that the wav
file is located in the first folder in the jar
, which is sounds
.
在这里,给定该路径的/sounds/
,因为当你解压jar
,你会看到该wav
文件位于在第一个文件夹jar
,这是sounds
。
回答by Sean
If you just want a beep or quick alert try
如果您只想发出哔哔声或快速警报,请尝试
Toolkit.getDefaultToolkit().beep();
回答by Erick Robertson
If you want to use the sound package to play an arbitrary sound file, you can use the javax.sound.sampled
package. Here is the code that will play a sound file:
如果要使用 sound 包播放任意声音文件,可以使用该javax.sound.sampled
包。这是将播放声音文件的代码:
private void playSound(File f) { Runnable r = new Runnable() { private File f; public void run() { playSoundInternal(this.f); } public Runnable setFile(File f) { this.f = f; return this; } }.setFile(f); new Thread(r).start(); } private void playSoundInternal(File f) { try { AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(f); try { Clip clip = AudioSystem.getClip(); clip.open(audioInputStream); try { clip.start(); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } clip.drain(); } finally { clip.close(); } } catch (LineUnavailableException e) { e.printStackTrace(); } finally { audioInputStream.close(); } } catch (UnsupportedAudioFileException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
回答by RealHowTo
You can generate your own sound if you looking for something less boring than a beep() without an external sound file.
如果您正在寻找比没有外部声音文件的 beep() 更无聊的东西,您可以生成自己的声音。
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);
}
}
More sound experiments here : Produce special sound effect
更多声音实验在这里: 产生特殊的声音效果