[英]What is wrong with my code so it can't play wav file?
I'm trying to use the code which available on: How can I play sound in Java? 我正在尝试使用以下代码: 如何在Java中播放声音? but I can't post question there since this is a new account and only have 1 reputation.
但我无法在此处发布问题,因为这是一个新帐户,并且只有1个声誉。
original code: 原始代码:
public static synchronized void playSound(final String url) {
new Thread(new Runnable() { // the wrapper thread is unnecessary, unless it blocks on the Clip finishing, see comments
public void run() {
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(Main.class.getResourceAsStream("/path/to/sounds/" + url));
clip.open(inputStream);
clip.start();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}).start();
}
and this is my code: 这是我的代码:
package sound_test;
import javax.sound.sampled.*;
public class Main {
public static synchronized void playSound(final String url) {
new Thread(new Runnable() {
public void run() {
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(Main.class.getResourceAsStream("/path/to/sounds/" + url));
clip.open(inputStream);
clip.start();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}).start();
}
public static void main(String[] args) {
// TODO code application logic here
playSound("C:\\warning_test.wav");
}
}
When I run the code i receive "null" as the output and no sound came out. 当我运行代码时,输出为“ null”,没有声音发出。 I've checked the file name and the path, it's correct.
我检查了文件名和路径,这是正确的。
screenshots: 屏幕截图:
http://puu.sh/pkYo http://puu.sh/pkYo
http://puu.sh/pkZl http://puu.sh/pkZl
Thank you in advance. 先感谢您。
you could do 你可以做
AudioInputStream inputStream=AudioSystem.getAudioInputStream(new File(url));
also add a delay after click.start(); ie Thread.Sleep(4000);
在
click.start(); ie Thread.Sleep(4000);
之后还添加了延迟click.start(); ie Thread.Sleep(4000);
click.start(); ie Thread.Sleep(4000);
or if you want to make sure it plays the entire audio sample you could use a simple snippet such as 或者,如果您想确保它能播放整个音频样本,则可以使用一个简单的代码段,例如
import javax.sound.sampled.*;
import java.io.File;
public class Main implements LineListener {
private boolean done = false;
public void update(LineEvent event) {
if(event.getType() == LineEvent.Type.STOP || event.getType() == LineEvent.Type.CLOSE) {
done = true;
}
}
public void waitonfinish() throws InterruptedException {
while(!done) {
Thread.sleep(1000);
}
}
public static void playSound(final String url) {
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File(url));
Main control = new Main();
clip.addLineListener(control);
clip.open(inputStream);
clip.start();
control.waitonfinish();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
public static void main(String[] args) {
// TODO code application logic here
playSound("C:\\warning_test.wav");
}
}
` `
You copied the code entirely without noticing the in the original, it points to 您完全复制了代码,而没有注意到原始代码,它指向
path/to/sounds
since you give it the full path, u should replace it with just url: 由于您提供了完整的路径,因此您应该仅用url替换它:
AudioInputStream inputStream = AudioSystem.getAudioInputStream(Main.class.getResourceAsStream(url));
EDIT: I tried here and got null as well. 编辑:我在这里尝试过,并且也为空。 I changed it to create the audioInput from a file:
我将其更改为从文件创建audioInput:
import java.io.File;
import javax.sound.sampled.*;
public class Main {
public static synchronized void playSound(final File file) {
new Thread(new Runnable() {
public void run() {
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(file);
clip.open(inputStream);
clip.start();
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getMessage());
}
}
}).start();
}
public static void main(String[] args) {
// TODO code application logic here
File file = new File("C:\\warning_test.wav");
playSound(file);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.