Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6386475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:01:52+00:00 2026-05-25T03:01:52+00:00

I’ve created a servlet which returns a stream (from an MP3 file) beginning at

  • 0

I’ve created a servlet which returns a stream (from an MP3 file) beginning at any given byte position requested by a client. This allows the client to start playback instantly at any given byte position without doing any local seek.

Now, I’ve got a slider which visualises the progress. I’m using the the current byte position to update the slider. However, I’d also like to show the current position in seconds.

This requires that the server can “convert” the current position in bytes to the position in milliseconds. The server could then just provided the stream start position in milliseconds as a response header.

Does anyone have experience as to how one can calculate the current position in bytes to milliseconds?

UPDATE

It is clear, from the comments, that there is no precise way to get convert bytes to milliseconds and vice versa without decoding the MP3 file up to the point (in milliseconds or bytes) and then determine how many bytes that have been read or milliseconds that have been played. However, this approach would obviously not perform too well considering a server where, say, 100 users would request files at the same time. The server would then have to decode the MP3 files up to the requested position, and then return streams from that point. I chose to exchange precision with performance, and took an approach which gives me approximate positions and which is more than good enough for a player which purpose is just to play a track (and not synch the audio against other sources down to the millisecond).

What I’ve done is that the player (on the client side) only cares about milliseconds (MS) now. That is, the current value and maximum value of the progress bar is in MS, and not bytes as it first did. To begin playback from any given position, the client requests the server (servlet) to provide the audio stream beginning at any given position in MS. The servlet use JAudioTagger to get details about the file, and then makes an approximate calculation as to what byte position the MS position corresponds to. I’ve tested it and it works well with CBR (constant bitrate) files. The approach will not work with VBR (variable bitrate) files as frame size can vary. Please note that this is just a player which purpose is to play music files. It’s not intended to synchronize the audio against some other media down to the MS. The code snipped which converts from MS to bytes is provided below.

UPDATE (July 3, 2012)

The servlet has been running with the below code for quite a while now, and things work very well. Thousands of MP3’s have been played, and the approximation from ms to bytes works fine.

UPDATE (January 3, 2017)

The servlet is still running with this exact same code, and hundreds of thousands of MP3s have been played nicely. During it’s time in production, no single complaint has been made with regards to playback and timing.

/**
 * Returns the approximate byte position for any given position in
 * milliseconds.
 *
 * http://www.java2s.com/Open-Source/Android/Mp3/needletagger/org/jaudiotagger/audio/mp3/MP3AudioHeader.java.htm
 * http://www.autohotkey.com/forum/topic29420.html
 *
 * @param   file the <code>File</code> for which the byte position for the
 *          provided position in milliseconds is to be returned.
 * @param   ms a <code>long</code> being the position in milliseconds for
 *          which the corresponding byte position is to be returned.
 * @return  a <code>long</code> being the byte position, or <b>-1</b> if the
 *          position in bytes could not be obtained.
 */
public static long getApproximateBytePositionForMilliseconds(File file, long ms) {

    long bytePosition = -1;

    try {

        AudioFile audioFile = AudioFileIO.read(file);
        AudioHeader audioHeader = audioFile.getAudioHeader();

        if (audioHeader instanceof MP3AudioHeader) {
            MP3AudioHeader mp3AudioHeader = (MP3AudioHeader) audioHeader;
            long audioStartByte = mp3AudioHeader.getMp3StartByte();
            long audioSize = file.length() - audioStartByte;
            long frameCount = mp3AudioHeader.getNumberOfFrames();
            long frameSize = audioSize / frameCount;

            double frameDurationInMs = (mp3AudioHeader.getPreciseTrackLength() / (double) frameCount) * 1000;
            double framesForMs = ms / frameDurationInMs;
            long bytePositionForMs = (long) (audioStartByte + (framesForMs * frameSize));
            bytePosition = bytePositionForMs;
        }

        return bytePosition;

    } catch (Exception e) {
        return bytePosition;
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T03:01:53+00:00Added an answer on May 25, 2026 at 3:01 am

    It doesn’t work like that. Even if your MP3 file is constant-bit-rate encoded, which byte position encodes which second in the stream is variable. (To be sure, VBR encoding makes it a lot more variable than CBR, but all the same.) The only way to reliably get this information is to actually decode the stream up to that point, which you probably don’t want to do. This is why even professional players such as XMMS cannot reliably update the slider when you skip around.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.