0

I am coding in java for android. the issue is how to get mp3 file size and its audio length before hand so that I can set my Progress bar / Seek bar. to respond to seeking events.

The answer can be a simple info in a header file or a algorithm.

I am using androids mediaplayer to stream and the only issue is seeking which requires both the above mentioned things.

Any help is appreciated.

I also tried manually looking into mp3 file and get the header to decode the mp3 length, with this noob code.

total = ucon.getContentLength(); //ucon is an HTTPURLconnection
is= ucon.getInputStream();

byte[] buffer = new byte[1024];  

is.read(buffer , 0, 1024);


int offset = 1024;
int loc = 2000;
//FrameLengthInBytes = 144 * BitRate / SampleRate + Padding
while(loc == 2000 && offset< total)
{
    for(int i = 0 ; i <1024 ;i++ )
    {
        if((int)buffer[i] == 255)
        {
            if((int)buffer[i+1] >= 224) 
             {
                 loc = i+1; 
                 break;
             }
        }
    }

    is.read(buffer , 0, 1024);
    offset = 1024 + offset;         
}

Coudn't find the pattern which marks a mp3 header (11111111 111xxxxx). Tried on different files. Can't find anything else to do.

Update 2:

Now I know I dont have to search for mp3 headers but ID3v2 headers. But still its done when streaming the file and on Android. I really hope someone helps and there are a lot of programs doing these things I wonder why would I have to do it the hard way.

ismail
  • 46,010
  • 9
  • 86
  • 95
Pinser
  • 1,898
  • 3
  • 29
  • 43
  • 1
    What MP3 library are you using? Show us your code and describe where you are stuck. – abelenky Dec 30 '10 at 04:05
  • Where are you streaming from? Internet or file? – gigadot Dec 30 '10 at 05:02
  • I am streaming mp3 over android using mediaplayer() for an Android device and connecting it with a mediacontroller ("http://developer.android.com/reference/android/widget/MediaController.html"). The problem is mediacontroller() cant set the mp3 songs length. It gives the following error: Not Sending buffering status because duration is unknown. Still got nothing. – Pinser Dec 30 '10 at 14:30
  • http://groups.google.com/group/android-developers/browse_thread/thread/b3437bb2324af077/c37cc39f350d38f2?show_docid=c37cc39f350d38f2 This is the same error as I am facing. If u guys suggest I will try to rewrite mediacontroller or mediaplayer class but I am just a fresh developer, would require loads of help. – Pinser Dec 30 '10 at 14:46
  • Sound like you can't get the audio length without downloading ther entire file http://stackoverflow.com/questions/624741/how-do-i-get-the-length-of-an-mp3-from-its-url – gigadot Dec 30 '10 at 15:23
  • Read answer posted by @bobince. maybe it helps – gigadot Dec 30 '10 at 15:24
  • Real Thanks for your fast response. I really need to get a third party library. – Pinser Dec 31 '10 at 02:07

1 Answers1

0

Well, I never knew about the ID3 tag system.

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
Pinser
  • 1,898
  • 3
  • 29
  • 43