MP3: a way to get position in milliseconds for any given byte position?

情到浓时终转凉″ 提交于 2019-12-30 06:56:09

问题


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:


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.




回答2:


An MP3 file or stream is a sequence of frames, where each frame consists of a mp3 header and a mp3 data part.

header and data part information are used to create a audio frame that "sounds like the original".

Therefore a position in the mp3 file or stream can't be converted to a timestamp in the resulting audio stream.




回答3:


The "hardest" way is doing a loop. Inside the loop you call your play() or player.play() method and after that put your thread to sleep for just enough milis so as the song can finish without starting the next song over it, but you must estimate for example a song with 7mb size has aproximately duration of about 3 minutes so you must sleep your thread in this space. Your song length is given by Files.readAllBytes(Path path). Something like that:

//i put 5 for example lets say 5 songs//
//1000000 bytes is 1MB the same calculation is the same for the rest  length numbers//
for(int i=0; i<5; i++){
   play();
   if(length>1000000 && length<7500000){
     try{
        Thread.sleep(milis);
     }
     catch(Exception e){
        e.printStackTrace;
}

You must create a byte[]array=Files.readAllBytes(Path path) and then a variable int length=array.length so as for every song to take its length.This method that i show is not super good but with a little attension it works fine enough.



来源:https://stackoverflow.com/questions/7267927/mp3-a-way-to-get-position-in-milliseconds-for-any-given-byte-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!