简体   繁体   中英

URL code assistance please short code (Newbie Java programmer)

I'm using NetBeans and trying to make a Jbutton play this .wav file that I have located in the build/classes/shadow part of my project. I want the .Jar to build with the .wav file so I could download the .Jar file on another computer and press the button and the music will play. The music is not locating or starting I'm not sure which it is, also please be easy on the terminology I am very very new.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:File Clap = new File("sanic.WAV");
display.setText("cum on step it up!!!"); 
} 
 public static void startMusic() {
 {
try{
  URL url = Shadow.class.getClassLoader().getResource("sanic.wav");
  play(); // NetBeans says it cannot find the symbol for play.


}catch(Exception e){ 
    }                                       
    }                                        
 }

I'm not sure in what part of your code are you using your URL... But I'd suggest you to use AudioInputStream ...

Regarding the problem finding the file... I had implemented something similar some time ago, while checking the code I have in my HDD, I found that this made it for me:

URL url = Shadow.class.getResource(myfile.wav);

But nowadays, I prefer working with this:

InputStream resourceAsStream = Shadow.class.getResourceAsStream(myfile.wav);

Hope it helps for what you need to do.

Regards and... happy coding :)

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