Search code examples
javaandroidstreamingmp3

How every one does mp3 streaming?


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.


Solution

  • Well, I never knew about the ID3 tag system.