It’s best to use getClass().getClassLoader().getResourceAsStream(String), where the String does not start with a forward slash and is relative to the root of the project.
Are we getting into the InputStream issues with audio again? I haven’t looked at your code, but if you are using a classLoader and getResourceAsStream() method, then you are probably returning an InputStream, and InputStreams often need to have mark/reset capabilities or they throw exceptions, and audio files (e.g. wav files) often don’t support this! And part of the problem is in part that it does work under some conditions, but is much less likely to work going forward. ra4king helped me figure out this problem when code of mine worked for his java 1.6, but bombed on his java 7.
If you need to get an AudioInputStream, can I recommend that you use the classLoader to return a URL of a String for your filename? I suspect your code will run more universally that way.
For example, where fileName is a String:
URL url = YourClass.class.getResource(fileName);
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
This bypasses the use of an InputStream, circumventing the need for mark/reset capabilities on the file.
I hope I haven’t “gone off” on something totally extraneous. Apologies if I have. But when I hear stuff about InputStreams when loading audio, I can’t help but think it is this problem. It has been showing up again and again–isn’t really well known yet.
that’s probably what i forgot, i was simply putting the source in as a string.
I’m sure you didn’t forget anything. They’re suggesting that TinySound should do this under the hood.
I’m pretty sure it doesn’t have to do with mark/reset capabilities. I don’t use those anywhere in my code and I can’t find anywhere in the documentation that says an InputStream needs to support them to open AudioInputStreams. I would assume that the AudioSystem.getAudioInputStream() function taking a URL opens an InputStream under the hood anyway. The function that takes a String looks like this:
/**
* Load a Music by a resource name. The resource must be on the classpath
* for this to work.
* @param name name of the Music resource
* @return Music resource as specified, null if not found/loaded
*/
public static Music loadMusic(String name) {
//check if the system is initialized
if (!TinySound.inited) {
System.err.println("TinySound not initialized!");
return null;
}
//check for failure
if (name == null) {
return null;
}
//check for correct naming
if (!name.startsWith("/")) {
name = "/" + name;
}
InputStream stream = TinySound.class.getResourceAsStream(name);
//check for failure to find resource
if (stream == null) {
System.err.println("Unable to find resource " + name + "!");
return null;
}
return TinySound.loadMusic(stream);
}
I’m reasonably confident he just didn’t have the audio file on his classpath or some such. I’ve added that null check on the stream with a message last night.
It goes in the same place as where i put external images, right?
It goes in the same place as where i put external images, right?
It really depends. That image directory may not be on your classpath. Perhaps you should try using the loadSound() function that takes a File first. It would look something like this:
Sound sound = TinySound.loadSound(new File("path/to/my/file/sound.wav"));
I’ve just tried this, and now it’s giving me the error :
Error getting resource stream!
mark/reset not supported
I’ve just tried this, and now it’s giving me the error :
Error getting resource stream!
mark/reset not supported
Well, the just shows that philfrei was right. Interesting. I’ll have to look into this. Can I ask what OS you’re running and what version of Java you’re using?
I just now found what philfrei was talking about in the documentation.
The implementation of this method may require multiple parsers to examine the stream to determine whether they support it. These parsers must be able to mark the stream, read enough data to determine whether they support the stream, and, if not, reset the stream’s read pointer to its original position. If the input stream does not support these operation, this method may fail with an IOException
I’ve opened an issue on the repo and will try to get it using URLs instead ASAP. This is system dependent, but it shouldn’t be terribly difficult to change.
I’m running 64 bit Windows 7 with Java 7.
I went ahead and switched it to use URLs as philfrei suggested. Another option would have been to wrap all InputStreams with BufferedInputStreams, but I like the URL solution. See if that works for you cubemaster21.
huzzah!! it works! Thank you very much!
Yeah that was what philfrei and I were talking about in that thread. A change in Java 7 caused AudioSystem to throw that strange mark/reset error when you give it an InputStream from getResourceAsStream(String).
I’m curious if TinySound has “matured” to the point where it could be added to the list of Resources on the “Java Gaming Resources” link.
http://www.java-gaming.org/index.php?action=resources
(link directly under the JGO logo)
I’m not waitting for that, I already use it ;D
I’m curious if TinySound has “matured” to the point where it could be added to the list of Resources on the “Java Gaming Resources” link.
http://www.java-gaming.org/index.php?action=resources
(link directly under the JGO logo)
I believe it’s pretty stable at this point and I would be happy to see it there, but I don’t know if it’s really my place to make such a decision. I have one more update that I was planning to push tonight and then I’ll just wait for issues to arise (if any).
I’m curious if TinySound has “matured” to the point where it could be added to the list of Resources on the “Java Gaming Resources” link.
http://www.java-gaming.org/index.php?action=resources
(link directly under the JGO logo)
Who makes the decision to put things on the list?
I really like this library. ;D It doesn’t have all the features that Paul’s has, but it works great! I use this for all of my projects. The interface is really simple and I am going to recommend this library to anyone that needs a simple library for playing sounds and music!
I really like this library. ;D It doesn’t have all the features that Paul’s has, but it works great! I use this for all of my projects. The interface is really simple and I am going to recommend this library to anyone that needs a simple library for playing sounds and music!
Thanks for the kind words. I’m glad you like it.
kuusisto, i’m here to give my feedback about the library. Well, i was looking for a simple library, and when i say “simple”, means simple functions, easy and at the same time powerful for my purpose. I tried the “Easy OGG” library, it’s a very good library, but in my case i was looking specifically for sound effects, and with the easy ogg library i had no success, cause in sound effects for games we often need to play fast sequences (like a shooter game for example), and the easy ogg isn’t good for this (correct me if i’m saying something wrong).
Well, in this case, your library covered everything i needed. At the moment it’s working good, no problems or bugs. For sound effects using the .ogg format, is the best that i found. Thank you very much.