代码
import javax.sound.sampled.*;
import java.io.*;
public class Tester {
static Thread th;
public static void main(String[] args) {
startNewThread();
while( th.isAlive() == true) {
System.out.println("sound thread is working");
}
}
public static void startNewThread() {
Runnable r = new Runnable() {
public void run() {
startPlaying();
}
};
th =new Thread(r);
th.start();
}
public static void startPlaying() {
try {
AudioInputStream ais = AudioSystem.getAudioInputStream(new File("d:/UnderTest/wavtester.wav"));
Clip clip = AudioSystem.getClip();
clip.open(ais);
clip.loop(-1); // keep playing the sound
} catch(Exception exc) { …Run Code Online (Sandbox Code Playgroud) 我想我的代码是好的,我的.jar文件及其好与它里面的.WAV ..但是,当我尝试加载它使用的getResourceAsStream我得到一个错误..
这是我的错误:
java.io.IOException: mark/reset not supported
at java.util.zip.InflaterInputStream.reset(Unknown Source)
at java.io.FilterInputStream.reset(Unknown Source)
at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(Unkno
wn Source)
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at operation.MainWindowOperations.prepareAudio(MainWindowOperations.java
:92)
at operation.MainWindowOperations.<init>(MainWindowOperations.java:81)
at graphics.LaunchGraphics.<init>(LaunchGraphics.java:25)
at run.RunApp.main(RunApp.java:14)
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
private void prepareAudio() {
try {
InputStream is = this.getClass().getClassLoader().getResourceAsStream("beep.wav");
inputStream = AudioSystem.getAudioInputStream(is);
clip = AudioSystem.getClip();
clip.open(inputStream);
} catch (Exception ex) {
ex.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
有人能帮我吗?非常感谢!!
我正在为我的学校项目重写我的 AudioManager 类,但遇到了一个问题。我的教授告诉我使用 Try-with-resources 块加载我的所有资源,而不是使用 try/catch(请参阅下面的代码)。我正在使用 javax.sound.sampled.Clip 中的 Clip 类,一切都与我的 PlaySound(String path) 方法完美配合,该方法使用 try/catch/ 如果我不 close() 剪辑。我知道如果我 close() 剪辑我不能再使用它。我已阅读 Oracle Docs for Clip 和 Try-with-resources,但找不到解决方案。所以我想知道的是:
是否可以使用 Try-with-resource 块在剪辑关闭之前播放/收听剪辑中的声音?
// Uses Try- with resources. This does not work.
public static void playSound(String path) {
try {
URL url = AudioTestManager.class.getResource(path);
try (Clip clip = AudioSystem.getClip()){
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
clip.open(ais);
clip.start();
}
} catch( LineUnavailableException | UnsupportedAudioFileException | IOException e) {
e.printStackTrace();}
}
// Does not use Try- with resources. This …Run Code Online (Sandbox Code Playgroud) JLayer我的音乐播放器项目有一个非常具体的问题。我想加入一些调节音量的东西,但似乎不太容易实现。
这是因为JLayer它本身不支持它。我Player从我的项目中排除了该类并更改了一些方法,并且它可以很好地播放mp3。为了调整音量,我向Player类中添加了以下方法:
public boolean setGain(float newGain) {
if (audio instanceof JavaSoundAudioDevice) {
System.out.println("InstanceOf");
JavaSoundAudioDevice jsAudio = (JavaSoundAudioDevice) audio;
try {
jsAudio.write(null, 0, 0);
} catch (JavaLayerException ex) {
ex.printStackTrace();
}
return jsAudio.setLineGain(newGain);
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
然后我提取JavaSoundAudioDevice、反编译并更改它:
public boolean setLineGain(float gain)
{
System.out.println("Vor Source");
if (source != null)
{
System.out.println("Nach Source");
FloatControl volControl = (FloatControl) source.getControl(FloatControl.Type.MASTER_GAIN);
float newGain = Math.min(Math.max(gain, volControl.getMinimum()), volControl.getMaximum());
volControl.setValue(newGain);
return true;
}
return false;
} …Run Code Online (Sandbox Code Playgroud) 我想分析麦克风输入的当前频率,以将我的 LED 与播放的音乐同步。我知道如何从麦克风捕获声音,但我不知道 FFT,我在寻找获取频率的解决方案时经常看到它。
我想测试某个频率的当前音量是否大于设定值。代码应该是这样的:
if(frequency > value) {
LEDs on
else {
LEDs off
}
Run Code Online (Sandbox Code Playgroud)
我的问题是如何在 Java 中实现 FFT。为了更好地理解,这里有一个指向 YouTube 视频的链接,它显示了我正在努力实现的目标。
整个代码:
public class Music {
static AudioFormat format;
static DataLine.Info info;
public static void input() {
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100, 16, 2, 4, 44100, false);
try {
info = new DataLine.Info(TargetDataLine.class, format);
final TargetDataLine targetLine = (TargetDataLine) AudioSystem.getLine(info);
targetLine.open();
AudioInputStream audioStream = new AudioInputStream(targetLine);
byte[] buf = new byte[256]
Thread targetThread = new Thread() { …Run Code Online (Sandbox Code Playgroud) 嗨,我需要从.wave文件中读取SampleRate,SignalFrequency和Amplitude.我怎么能用JavaSound做到这一点?
如何读取AudioInputStream特定数量的字节/微秒位置?例如 :
AudioInputStream ais = AudioSystem.getAudioInputStream( new File("file.wav") );
// let the file.wav be of y bytes
Run Code Online (Sandbox Code Playgroud)
现在我想获得一个AudioInputStream具有高达某些x字节数据的数据x < y.
我怎样才能做到这一点 ?
我一直在努力思考但没有任何方法可以做到这一点?
这是堆栈跟踪:
java.io.IOException: Resetting to invalid mark
at java.io.BufferedInputStream.reset(BufferedInputStream.java:433)
at org.tritonus.share.sampled.file.TAudioFileReader.getAudioInputStream(TAudioFileReader.java:324)
at javazoom.spi.mpeg.sampled.file.MpegAudioFileReader.getAudioInputStream(Unknown Source)
at javazoom.spi.mpeg.sampled.file.MpegAudioFileReader.getAudioInputStream(Unknown Source)
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1179)
at javazoom.jlgui.basicplayer.BasicPlayer.initAudioInputStream(Unknown Source)
at javazoom.jlgui.basicplayer.BasicPlayer.initAudioInputStream(Unknown Source)
at javazoom.jlgui.basicplayer.BasicPlayer.open(Unknown Source)
at BasicPlayerDemo.play(BasicPlayerDemo.java:49)
at BasicPlayerDemo.main(BasicPlayerDemo.java:24)
Run Code Online (Sandbox Code Playgroud)
似乎其他人也有这个问题:
有什么理由吗?我正在尝试使用JavaZoom类创建一个简单的Java Swing音乐播放器.
我的项目是"对阿塞拜疆语言的语音识别".我必须编写一个将wav文件转换为字节数组的程序.
如何将音频文件转换为byte []?
我正在尝试组装一个MIDI序列的图形表示,我希望一条垂直线在序列播放时水平移动到面板上,反映序列中的实际位置.我知道我应该用类似的东西getTickPosition()来提供垂直线的位置.
但是如何触发这些事件以使垂直线知道重绘?我是否创建了一个特殊的侦听器,以便以某种方式触发?