• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting resource from jarfile  RSS feed

 
Mark Hedge
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been banging my head against this one all last night, and my googlefu has failed me it seems.

I've got 2 wavs and an mp3 bundled in an executable jarfile, all well and good. The wavs play. But the mp3 does not.

I needed some special code using javax.media to get the mp3 working, whereas the wavs are just AudioClips and do work.

The mp3 plays fine when I'm running it in the IDE, it just doesn't work when its all jarred up. Heres the code (most of it not mine) :-

package asteroids;

import javax.media.*;
import java.io.*;
import java.net.*;


public class PlayMP3Thread extends Thread {

private URL url;
private Player player;
private boolean endthread;

public PlayMP3Thread(File mp3) {
try {
this.url = mp3.toURL();
} catch (Exception e) {
System.err.print(e.toString());
}
}

public PlayMP3Thread() {
Class thisClass = this.getClass();
try {
url = thisClass.getResource("background.mp3");
} catch (Exception e) {
e.printStackTrace();
}
}


public void run() {
try {
MediaLocator ml = new MediaLocator(url);
player = Manager.createPlayer(ml);
player.addControllerListener(
new ControllerListener() {
public void controllerUpdate(ControllerEvent event) {
if (event instanceof EndOfMediaEvent) {
player.stop();
System.err.println("End of media?");
}
}
}
);
player.realize();
player.start();
} catch (Exception e) {
e.printStackTrace();
}

}

public void playerstart() {
try {
player.start();
} catch (Exception e) {
e.printStackTrace();
}
}

public void playerstop() {
try {
player.stop();
} catch (Exception e) {
e.printStackTrace();
}
}


public boolean ready(){
try {
int state = player.getState();
return state == Player.Started;
} catch (Exception e) {
return false;
}
}

public int playerState() {
return player.getState();
}
}

I've bolded the relevant bit. thisClass.getResource is the method I have to use to dig out a resource from a jarfile, right?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try prefixing the filename with a slash, i.e. "/background.mp3".
 
Mark Hedge
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried various filename permutations already - doesn't work.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about obtaining the mp3 entry with a JarEntry class ? You are having trouble obtaining the resource, not playing it, am I correct ?
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The mp3 plays fine when I'm running it in the IDE, it just doesn't work when its all jarred up.


And just to make sure you have the mp3 file at the right place in the jar, do the following:

- From the command prompt, go to the folder which contains your jar
- Run the following command:



This will print out the contents of the jar file. This will let you know where exactly the mp3 file is placed.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!