tumtumtum / StreamingKit

A fast and extensible gapless AudioPlayer/AudioStreamer for OSX and iOS (iPhone, iPad)
Other
2.43k stars 525 forks source link

Why the totoal duration Of MP3 is more than the right duration? #241

Open gilnuy opened 8 years ago

gilnuy commented 8 years ago

for example: the duration is 1:30:10 streammingkit return is 1:30:40.

how could i get the right duration? please help me,thank you!

yinchengvy commented 8 years ago

I have the same problem with you.

tumtumtum commented 8 years ago

The duration is calculated based upon the average bitrate of the file. Without reading the whole file (which defeats the purpose of seek-less streaming) it's not always possible to get a perfect approximation especially for VBR files.

In general, the error will tend towards 0 the longer you play the file (this means that the duration changes as the file is playing).

You could try increasing STK_MAX_COMPRESSED_PACKETS_FOR_BITRATE_CALCULATION. For MP3 files it may also be possible to read the length from the ID3 tags.

Cheers.

yinchengvy commented 8 years ago

Thx, I've tried it, but it does not work.

yinchengvy commented 8 years ago

I've modified like this, and it does work well.

-(double) calculatedBitRate
{
    double retval;

    if (packetDuration > 0.0)
    {
        if (processedPacketsCount > STK_BIT_RATE_ESTIMATION_MIN_PACKETS_PREFERRED || (audioStreamBasicDescription.mBytesPerFrame == 0 && processedPacketsCount > STK_BIT_RATE_ESTIMATION_MIN_PACKETS_MIN))
        {
            double averagePacketByteSize = (processedPacketsSizeTotal * 1.0) / processedPacketsCount;

            retval = averagePacketByteSize / packetDuration * 8;

            return retval;
        }
    }

    retval = (audioStreamBasicDescription.mBytesPerFrame * audioStreamBasicDescription.mSampleRate) * 8;

    return retval;
}
tumtumtum commented 8 years ago

Ahh, missed the int->double precision problem. Good catch. Fixed in HEAD.