SD library determining file size accurately of mp3 files


I'm using an Arduino Uno. I need to determine the file size of an mp3 file, in order to determine the length of the track. I have the math to determine the length of track with the file size. I'm using the SD library.

The issue I have, is that size() returns the accurate size of text files, but not mp3 files.

 myFile =;
  unsigned long siz = myFile.size();    
   Serial.print("File size is: "); Serial.println(siz);

// ....
 String prefix = "/tunes/";
 String suffix = ".mp3";
 String file = prefix + rand_indice + suffix;
 Serial.print("track is: "); Serial.println(file);
 myFile =;
 siz = 0;
 siz = myFile.size();
 Serial.print("Track size is: "); Serial.println(siz);

The Serial out put is:

File size is: 376
track is: /tunes/5.mp3
Track size is: 16796674

The file size from a command prompt for the mp3 is: 350666

Can anyone shed some light on this?


I can't help you with your question... My C++ is too rusty.

Are you sure you're actually reading the correct file? Can you play the file using the same file-opening syntax. Or, Maybe you can read the first-several bytes and display them on the serial monitor, then compare that to what you see on a computer (a hex editor would make that easy). Or, read the file one byte at a time and count-up the bytes.

order to determine the length of the track

By "length", do you mean playing time? That can be tricky with MP3. You can approximate it if you know the file-size and bitrate, but with variable bitrate MP3s you need to know the average bitrate for the particular file.

To get accurate playing time, I think you have to analyze the MP3 frame-by-frame. I've seen an example of that once, but I can't find it right now.

There may also be a playing-time "tag", but they are not always correct.

Can anyone shed some light on this?

Probably not without the actual mp3 file and the complete program, to be able to actually recreate the problem.

Can anyone shed some light on this?

Learn about standard string handling in the C/C++ programming language and nullterminated C-style sttrings!

I think your problems are caused because you are using Arduino "String" objects with the SD library, which takes null-terminated strings as file name parameters in function calls and NOT "String! objects.