我正在研究一个Java程序,我真的需要能够以一定的频率和持续时间播放声音,类似于c#方法System.Beep,我知道如何在C#中使用它,但我找不到一种在Java中执行此操作的方法.是否有一些等效或其他方式来做到这一点?
using System;
class Program
{
static void Main()
{
// The official music of Dot Net Perls.
for (int i = 37; i <= 32767; i += 200)
{
Console.Beep(i, 100);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Java生成蜂鸣声.我在SO上找到了这个答案.
我正在使用该答案的代码来产生哔哔声.代码是:
import javax.sound.sampled.*;
public class Sound
{
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,8,1,true,false);
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] = …Run Code Online (Sandbox Code Playgroud) 我想知道是否有一个库或某些东西在给定的采样率(20-20,000 Hz)下播放声音.实际上,我发现了一些东西,但我不明白我是如何让它发挥作用的!