简体   繁体   中英

Audio not playing with JAR File

I created a project that plays audio within the netbeans IDE. Those audio files were placed in the Classes folder. Although when I created it as a JAR file, it was unable to locate the audio files. I even copy and pasted the files inside the new dist folder. Here is a snippet of code:

    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!");
  }   
}

If you want to embedd the audio file in your program it's must be placed inside the src folder in a package.
For example I'll demonstrate a code I use to set icons to buttons (should work for audio files as well) :
While creating the JFrame I wrote :

jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Icon/PatientBig.png")));

I have in my project a package called GUI with a subpackage called Icons where my icons exist and they all are in src folder.
When you using getClass().getResource function , I prefer to use an absolute path.
After seeing your respone I have noticed that you keep using . in the begining of the class path, I copied the snippet you published and removed the . from the begining of the path and placed my audio file bark.wav in the src folder in the default package and it worked

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();
    }
}

Then I placed the audio file inside a package called test1 and modified the path in getResourceAsStream function and again it worked:

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();
    }
}

The Most important thing is to remove . from the path

尝试这个

InputStream in = getClass().getResourceAsStream("/beep39.wav"); 

I think you need to bypass use of the InputStream. When running the getAudioInputStream method, using InputStream as a parameter triggers markability and resetability tests on the audio file. Audio files usually fail these tests. If you create your AudioInputStream with a URL or File parameter, these tests are circumvented. I prefer URL as it seems more robust and can "see into" jars.

URL url = getClass().getResource("./beep39.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(url);

Then, in a while loop, you would execute a read method on the AudioInputStream and send the data to a SourceDataLine.

The Java Tutorials covers this in their audio trail . This link jumps into the middle of the tutorials.

AFAIK, there is no "AudioPlayer" in the Java 7 SDK.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM