Ogg loading - java.lang.StringIndexOutOfBoundsException: String index out of range: -1

When i try to load a certain ogg file i get the following error

java.lang.StringIndexOutOfBoundsException: String index out of range: -1

at java.lang.String.substring(String.java:1938)

at de.jarnbjo.vorbis.CommentHeader.(CommentHeader.java:71)

at de.jarnbjo.vorbis.VorbisStream.(VorbisStream.java:86)

at com.jme3.audio.plugins.OGGLoader.load(OGGLoader.java:160)

at com.jme3.asset.DesktopAssetManager.loadAsset(DesktopAssetManager.java:224)

at com.jme3.audio.AudioNode.(AudioNode.java:89)

at mygame.Main.initViewportLightCamPostproAudio(Main.java:857)

at mygame.Main.simpleInitApp(Main.java:195)

at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:185)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:136)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:193)

at java.lang.Thread.run(Thread.java:619)

Here is the file


The encoder that created this file is from 2002. It seems to play fine in all my players, so this might just be a small bug in the decoder we are using.

Still a length value of -1 in the comment header is not supposed to be allowed, since the length is unsigned, its actually 0xFFFFFFFF which I think should be an illegal value.

I was able to clear the comment header without having to re-encode the file, here’s the fixed version:

its’ only 1.6 MB now, is it quality is same?

Then, what should I used to encode to avoid this error.

The file you gave me was 1.6 MB (it was encoded with 96 kbps), all I did was clear the comment header.

If you’re creating your own sound files, use the latest encoder from Xiph.org

Can you suggest a particular application. I dont know how to use a encoder lib to convert files.

You can use audacity to convert WAV/MP3 files to OGG.

I recommend using the beta version as it makes higher quality ogg files.

Meanwhile me and Normen are are planning on integrating a converter into jMP, not sure if it is needed or not.

Well, it would be a handy feature, but, not an absolute necessity.


This post is in case someone else finds this thread while searching for this same problem. The developers here appear to be aware of this but it is not an issue with jMonkeyEngine. However it is very obscure and still exists in the wild. Here is what is going on:

java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(Unknown Source)
at de.jarnbjo.vorbis.CommentHeader.(CommentHeader.java:71)
at de.jarnbjo.vorbis.VorbisStream.(VorbisStream.java:86)
at client.Main.main(Main.java:78)

This occurs when loading an OGG Vorbis encoded file which has an improperly formatted comment. Under the standard, the OGG Vorbis comment block contains a vendor string followed by a list of key=value pairs. If one of these key=value pairs does not actually contain an “=” character, an indexOf() call returns -1 and a following substring() call throws the above exception. It is possible for this to happen because the jOgg library strictly interprets the comment block while some other libraries treat this as a warning or not at all.

The standard can be found at http://www.xiph.org/vorbis/doc/v-comment.html. An offending OGG file has a comment block which successfully passes all 9 of the parsing steps for the primary structure. However, when the vectors from step 6 are parsed into key=value pairs, an offending OGG file may contain an entry without an “=” sign. The easiest albeit hackiest way to fix such a file is to open it up, find the comment block, and throw an equals sign in the pair which is malfunctioning.

Hex sample:
03 76 6F 72 62 69 73 1D 00 00 00 58 69 70 68 2E 4F 72 67 20 6C 69 62 56 6F 72 62 69 73 20 49 20
32 30 30 32 30 37 31 37 01 00 00 00 19 00 00 00 53 6F 6E 79 20 4F 67 67 20 56 6F 72 62 69 73 20
31 2E 30 20 46 69 6E 61 6C 01 05 76 6F 72 62 69 73

WARNING: Back up any file you edit before attempting this. You may be causing more problems if you do not understand what you are doing.

Briefly: the comment block is near the top of the OGG Vorbis file and it starts with “03vorbis” and ends with “vorbis”. Between this is a string, number, and and maybe more strings. Strings will start with a four-byte length and are not terminated. In the above hex the first length is “1D 00 00 00” or 29 bytes. After this is a number which tells us how many more strings to read. In this case “01 00 00 00” = 1 more string. This of course is followed by the length and string for each set, and from here on out each string needs to be a key=value pair. In the above hex our next string is “19 00 00 00” = 25 bytes long but does not contain character 0x3d (=). The j-Ogg library would crash here, having rightfully expected to find an = sign in the string. To fix this issue, find a place within these 25 bytes and change a character to “=”.

There is only one other tutorial on the internet for solving this problem and it requires downloading a separate fixing tool and running it. I believe it is more important to identify which encoders are producing OGG Vorbis files with bad or invalid comment blocks and determine if their vendors have since fixed it. One file which I have in my possession has the offending comment “Sony Ogg Vorbis 1.0 Final.”

I hope this information can save someone else a few hours or more. Good luck.