Question

I'm trying to manipulate ID3 tags in MP3 Files using Java ID3 Tag Library. However, when i try to do ANYTHING using an "MP3File" class, i'm getting IOException: Negative Seek Offset;

Here's an example code and the stacktrace:

package com.test;

import java.io.File;
import java.io.IOException;

import org.farng.mp3.MP3File;
import org.farng.mp3.TagException;

public class MP3Test {

    public static void main(String[] args) throws IOException, TagException {

        File f = new File("test.mp3");

        MP3File mp = new MP3File(f);

        System.out.println(mp.getFrequency());
    }


    }
--------------------------------------------------------------------------------

Exception in thread "main" java.io.IOException: Negative seek offset
    at java.io.RandomAccessFile.seek(Native Method)
    at org.farng.mp3.id3.ID3v1.seek(Unknown Source)
    at org.farng.mp3.id3.ID3v1.read(Unknown Source)
    at org.farng.mp3.id3.ID3v1.<init>(Unknown Source)
    at org.farng.mp3.MP3File.<init>(Unknown Source)
    at org.farng.mp3.MP3File.<init>(Unknown Source)
    at com.test.MP3Test.main(MP3Test.java:15)

As you see, even an basic operation throws me an exception, making it really hard for me to really develop something... The MP3 file that i'm trying to read is working perfectly fine. So, any ideas on why is this happening and/or how to solve it??

No correct solution

OTHER TIPS

So, any ideas on why is this happening and/or how to solve it??

At the risk of stating the obvious, there are 2 possible cause for the problem:

  • The MP3 file is broken in some subtle way that doesn't prevent it from "working" ... which I presume means playing in some non-Java player.
  • The library is buggy and it is not handling something properly.

Solutions?

  • Try a different MP3 file.
  • Try multiple files from different sources, in case it is something to do with MP3 files produced by a particular piece of software.
  • Run the library using a Java debugger to find out what is actually causing the exception. This will probably entail reading the MP3 specs to decide whether there is some mismatch between 1) the code, 2) the spec and 3) the bytes in the file.

You asked this as a programming question so I assume that you are a programmer, and are capable of troubleshooting in general, and in particular in finding / reading specs, and debugging Java.


OK ... since you don't appear to have made any progress with this yourself, I took a look at the source code I found here. (Of course, this might be the wrong version, but you didn't say what version of the library you were using ...)

It appears that the problem is occurring in line 309 of org.farng.mp3.id3.ID3v1.java in the seek method:

// If there's a tag, it's 127 bytes long and we'll find the tag
file.seek(file.length() - 128);

Now the only way that that could possibly give a negative seek offset exception is if file.length() returns a file length less than 128. That should never happen for a valid MP3 file.

So my guess is that either you are trying to read truncated MP3 files, or files that are not MP3 files at all, or you are trying to read the files from some device/file system that doesn't report file sizes properly.

(It is worth reading the javadocs for File.length() for clues as to why it might return a strange value; e.g. zero.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top