简体   繁体   中英

Background music using java JLayer plays in eclipse and runnable jar but doesn't play when a client downloads and runs it

I currently have an LWJGL game with background music. When I run my game in Eclipse everything seems fine. The music also works when I export it as a runnable jar. However, when I put my game on a web server and a client downloads and runs it, the music doesn't play. I initially had the following:

try{
    File file = new File ("/Users/me/Documents/workspace/LWJGL-T/src/file.mp3");
    FileInputStream fileStream = new FileInputStream (file);
    BufferedInputStream bufferStream = new BufferedInputStream (fileStream);
    Player player = new Player (bufferStream);
    player.play();
}
catch (Exception e){
   System.err.println (e.toString());
}

After some research, I found out that FileInputStream shouldn't be used with mp3 files so I implemented the following, which didn't work at all.

    InputStream fileStream = this.getClass().getResourceAsStream("/Users/me/Documents/workspace/LWJGL-T/src/file.mp3");

Any help would be appreciated; thank you!

As stated in the comments below your original post, the file you're trying to access is only stored on your computer. You'll have to include the file in your jar (see this post for setting that up) and after that, the line below should work:

InputStream fileStream = this.getClass().getResourceAsStream("/file.mp3");

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