音频不播放 JAR 文件
Audio not playing with JAR File
我创建了一个在 netbeans 中播放音频的项目 IDE。这些音频文件放在 类 文件夹中。
虽然当我将它创建为 JAR 文件时,它无法找到音频文件。我什至将文件复制并粘贴到新的 dist 文件夹中。
这是一段代码:
private void playSound39()
{
try
{
/**Sound player code from:
http://alvinalexander.com/java/java-audio-example-java-au-play-sound
*/
// the input stream portion of this recipe comes from a javaworld.com article.
InputStream inputStream = getClass().getResourceAsStream("./beep39.wav");
AudioStream audioStream = new AudioStream(inputStream);
AudioPlayer.player.start(audioStream);
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null,"Audio file not found!");
}
}
如果你想在你的程序中嵌入音频文件,它必须放在包中的 src
文件夹中。
例如,我将演示用于将图标设置为按钮的代码(也适用于音频文件):
在创建 JFrame 时我写道:
jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Icon/PatientBig.png")));
我的项目中有一个名为 GUI
的包和一个名为 Icons
的子包,其中存在我的图标,它们都在 src
文件夹中。
当你使用 getClass().getResource
功能时,我更喜欢使用绝对路径。
看到您的回复后,我注意到您在 class 路径的开头继续使用 .
,我复制了您发布的代码段并从路径开头删除了 .
并放置我的音频文件 bark.wav
在默认包的 src
文件夹中并且它有效
public class test {
private void playSound39() {
try {
/**
* Sound player code from:
* http://alvinalexander.com/java/java-audio-example-java-au-play-sound
*/
// the input stream portion of this recipe comes from a javaworld.com article.
InputStream inputStream = getClass().getResourceAsStream("/bark.wav");
AudioStream audioStream = new AudioStream(inputStream);
AudioPlayer.player.start(audioStream);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Audio file not found!");
}
}
public static void main(String[] args){
new test().playSound39();
}
}
然后我将音频文件放在一个名为 test1
的包中,并修改了 getResourceAsStream
函数中的路径,它再次起作用:
public class test {
private void playSound39() {
try {
/**
* Sound player code from:
* http://alvinalexander.com/java/java-audio-example-java-au-play-sound
*/
// the input stream portion of this recipe comes from a javaworld.com article.
InputStream inputStream = getClass().getResourceAsStream("/test1/bark.wav");
AudioStream audioStream = new AudioStream(inputStream);
AudioPlayer.player.start(audioStream);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Audio file not found!");
}
}
public static void main(String[] args){
new test().playSound39();
}
}
最重要的是从路径
中删除.
试试这个
InputStream in = getClass().getResourceAsStream("/beep39.wav");
我认为您需要绕过 InputStream 的使用。 运行 getAudioInputStream 方法时,使用 InputStream 作为参数触发音频文件的可标记性和可重置性测试。音频文件通常无法通过这些测试。如果您使用 URL 或 File 参数创建 AudioInputStream,则会绕过这些测试。我更喜欢 URL 因为它看起来更健壮并且可以 "see into" 罐子。
URL url = getClass().getResource("./beep39.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
然后,在 while 循环中,您将在 AudioInputStream 上执行读取方法并将数据发送到 SourceDataLine。
Java 教程在其 audio trail 中涵盖了这一点。这 link 跳到教程的中间。
据我所知,Java 7 SDK 中没有 "AudioPlayer"。
我创建了一个在 netbeans 中播放音频的项目 IDE。这些音频文件放在 类 文件夹中。 虽然当我将它创建为 JAR 文件时,它无法找到音频文件。我什至将文件复制并粘贴到新的 dist 文件夹中。 这是一段代码:
private void playSound39()
{
try
{
/**Sound player code from:
http://alvinalexander.com/java/java-audio-example-java-au-play-sound
*/
// the input stream portion of this recipe comes from a javaworld.com article.
InputStream inputStream = getClass().getResourceAsStream("./beep39.wav");
AudioStream audioStream = new AudioStream(inputStream);
AudioPlayer.player.start(audioStream);
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null,"Audio file not found!");
}
}
如果你想在你的程序中嵌入音频文件,它必须放在包中的 src
文件夹中。
例如,我将演示用于将图标设置为按钮的代码(也适用于音频文件):
在创建 JFrame 时我写道:
jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Icon/PatientBig.png")));
我的项目中有一个名为 GUI
的包和一个名为 Icons
的子包,其中存在我的图标,它们都在 src
文件夹中。
当你使用 getClass().getResource
功能时,我更喜欢使用绝对路径。
看到您的回复后,我注意到您在 class 路径的开头继续使用 .
,我复制了您发布的代码段并从路径开头删除了 .
并放置我的音频文件 bark.wav
在默认包的 src
文件夹中并且它有效
public class test {
private void playSound39() {
try {
/**
* Sound player code from:
* http://alvinalexander.com/java/java-audio-example-java-au-play-sound
*/
// the input stream portion of this recipe comes from a javaworld.com article.
InputStream inputStream = getClass().getResourceAsStream("/bark.wav");
AudioStream audioStream = new AudioStream(inputStream);
AudioPlayer.player.start(audioStream);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Audio file not found!");
}
}
public static void main(String[] args){
new test().playSound39();
}
}
然后我将音频文件放在一个名为 test1
的包中,并修改了 getResourceAsStream
函数中的路径,它再次起作用:
public class test {
private void playSound39() {
try {
/**
* Sound player code from:
* http://alvinalexander.com/java/java-audio-example-java-au-play-sound
*/
// the input stream portion of this recipe comes from a javaworld.com article.
InputStream inputStream = getClass().getResourceAsStream("/test1/bark.wav");
AudioStream audioStream = new AudioStream(inputStream);
AudioPlayer.player.start(audioStream);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Audio file not found!");
}
}
public static void main(String[] args){
new test().playSound39();
}
}
最重要的是从路径
中删除.
试试这个
InputStream in = getClass().getResourceAsStream("/beep39.wav");
我认为您需要绕过 InputStream 的使用。 运行 getAudioInputStream 方法时,使用 InputStream 作为参数触发音频文件的可标记性和可重置性测试。音频文件通常无法通过这些测试。如果您使用 URL 或 File 参数创建 AudioInputStream,则会绕过这些测试。我更喜欢 URL 因为它看起来更健壮并且可以 "see into" 罐子。
URL url = getClass().getResource("./beep39.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
然后,在 while 循环中,您将在 AudioInputStream 上执行读取方法并将数据发送到 SourceDataLine。
Java 教程在其 audio trail 中涵盖了这一点。这 link 跳到教程的中间。
据我所知,Java 7 SDK 中没有 "AudioPlayer"。