I have an mp3 file that i am trying to play from a source package in eclipse (java) for some reason it plays fine when i run it in eclipse but when i try to play it from a .jar it doesn't play. The images show up fine but the sound doesn't, i think i may have to recode the sound file like an image file but i'm not sure how here's the code for the Image:
JLabel TextBox = new JLabel(new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/Images/TextBox.png"))));
and the Sound
Player player = Manager.createPlayer(new MediaLocator(getClass().getClassLoader().getResource("Sounds/Bonsai.mp3")));
thanks in advance!
Try getting the resource as a Stream. You can retrieve a file this way.
InputStream fis =
ClassLoader.getSystemClassLoader().getResourceAsStream("/Sounds/bonsai.mp3");
p = new Player(fis);
p.play();
The:
getClass().getClassLoader().getResource(....)
does indeed return a URL. But MediaLocator can not use URL's from the form jar:file:/
I recommend to copy the file from the jar to a "Temp" filesystem and the stream it from there.
You can always delete it when you done, if that is needed.
Related
I wrote some code which plays some .mp4 videos inside a JFrame. I am using vlcj-3.0.1 and inside eclipse everything works perfectly.
But when i am exporting the Java project as an runnable JAR file and putting the res folder seperatly in the same directory as the JAR file, the videos arent played anymore. I must been doing something wrong with the filepath, but i don't know what i am doing wrong.
I already looked up google and found a thread with the exact same problem:
VLCJ - playing a video from the "res" folder works great in eclipse, but not from the executable JAR file
i tried using the recommendations from caprica but i had no success.
// Attributes
Canvas c = new Canvas();
MediaPlayerFactory mpf = new MediaPlayerFactory();
EmbeddedMediaPlayer emp = mpf.newEmbeddedMediaPlayer();
String mrl = new File("res/video/beach.mp4").getAbsolutePath();
// For Seeing the FilePath, so i can place the res folder correctly after
//creating the runnable jar file
System.out.println(mrl);
// Giving the Libary for VLCJ Plugin
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "lib");
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
// Setting up the player, didnt including the jframe here, since its working
emp.setVideoSurface(mpf.newVideoSurface(c));
emp.setEnableMouseInputHandling(false);
emp.setEnableKeyInputHandling(false);
emp.prepareMedia(mrl);
emp.setRepeat(true);
emp.play();
I expected that a JFrame opens up and plays the video on a loop.The JFrame pops up but i have just a black screen...(i set my jframe background color to black).
thanks for replying. that was exactly the problem.
like you already mentioned, i extracted the resources to a seperate folder and let the runnable JAR file reach to the "outside" media.
it worked by an solution caprica mentioned in the other thread:
String mrl = new File("res/media/video.mp4").getAbsolutePath();
now things get a little bit different. since i wanted to load the resources from the locations where the jar file is saved (so the jar file dynamically adapts to the location of itself and the added resources.
i added the following lines:
String jarDir = new File(ClassLoader.getSystemClassLoader().getResource(".").getPath()).getAbsolutePath();
String dir = jarDir;
String path = "res/video/beach.mp4"
mpf = new MediaPlayerFactory();
emp = mpf.newEmbeddedMediaPlayer();
emp.prepareMedia(dir + path);
emp.setRepeat(true);
emp.play();
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");
I have been trying to set the album art of a mp3 file using the mp3agic library in java. The following code snippet is what i used to do so. Unfortunately this isnt working. The saved image file does not contain the album art nor the set title. Also, the program compiles with no error.
File img = new File("img.jpg");
imgLink[1]="http://"+imgLink[1];
URL url = new URL(imgLink[1]);
FileUtils.copyURLToFile(url, img);
byte[] bytes = new byte[(int) img.length()];
id3v2Tag.setTitle("Something");
id3v2Tag.setAlbumImage(bytes, "image/jpg");
mp3file.save("something.mp3");
Any help regarding this issue would be much appreciated.
The above code works. It wasn't showing properly on the folder I was working with. When I opened the file with iTunes it showed up. It's probably some fault with windows.
I would like to ask if its possible to put text files into my jar, I use them to make my map in my game, but users can get Highscores. now I want to save the Highscores with the map, so I have to save the map on the user their PC. Is there any way how I could do this? I've searched the internet for some ideas but I could not find anything that even came close to what I've wanted. I only had 3/4th of a year java so I don't know much about these things, everything that happens outside the debug of eclipse are problems for me(files are mainly one of those things, null exceptions, etc).
The main question now.
Is it possible to do? If yes, do you have any terms I could search on, or some sites/guides/tutorials? If no, is there any other way how I could save the highscores?
EDIT:
to make clear
Can I get the text file (the text inside the file) to be extracted to a different file in like the home directory of my game (where I save the settings and stuff) the basic maps are inside the jar file, so I want them to be extracted on the first start-up of the program
Greetings Carolien
"extracted to a different file in like the home directory of my game (where i save the settings and stuff) the basic maps are inside the jar file, so i want them to be extracted on the first startup of the program"
You can get the URL by using getClass().getResource()
URL url = getClass().getResource("/res/myfile.txt");
Then create a File object from the URI of the URL
File file = new File(url.toURI());
Then just perform your normal file operations.
if (file.renameTo(new File(System.getProperty("user.home") + "\\" + file.getName()))) {
System.out.println("File is moved successful!");
} else {
System.out.println("File is failed to move!");
}
Assuming your file structure is like below, it should work fine
ProjectRoot
src
res
myfile.txt
Note: the above is moving the entire file. If you want to extract just the data inside the file, then you can simple use
InputStream is = getClass().getResourceAsStream("/res/myfile.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
The just do normal IO operation with the reader. See here for help with writing the file.
I've been trying to simply load a wav file into an AudioChannel object and play it but it keeps giving me an error stating "Unable to load sound test.wav"
What I'm doing is:
AudioChannel currentsound;
Ess.start(this);
currentsound = new AudioChannel("test.wav");
currentsound.play();
The audio file is in the working directory too, any idea why it won't load?
By working directory do you mean the data sub-directory in the sketch? Processing reads from 'data/' in the Sketch folder. Saving takes place in the Sketch folder itself.
Try this:
currentsound = new AudioChannel("../test.wav");