Mp3 file use Fmod :: sound getlength return incorrect duration

I am creating my sounds like this:

FMOD_CREATESOUNDEXINFO exinfo;
memset(&exinfo, 0, sizeof(FMOD_CREATESOUNDEXINFO));
exinfo.cbsize = sizeof(FMOD_CREATESOUNDEXINFO);
exinfo.suggestedsoundtype = FMOD_SOUND_TYPE_MPEG;
exinfo.filebuffersize = 1024*128 * 2;

_system->createSound(aiInPath,  FMOD_2D | FMOD_CREATESTREAM | FMOD_ACCURATETIME | FMOD_NONBLOCKING, &exinfo, &_sound);
_system->playSound(_sound, 0, true, &_channel);

unsigned int len = 0;
_sound->getLength(&len, FMOD_TIMEUNIT_MS);

The actual length of the mp3 file is 15 seconds, but this is giving me 30 seconds. Is there something I’m missing here?

FMOD Studio API Version : iOS 2.00.07

this is Mp3 file

Asking for help

Using the code you provided I was able to get a length of 15908 ms.

Oh No, can I see your complete code~
I run it on IOS.

I used the code you provided in our ‘play_sound’ example included in the FMOD API download, except I removed the system->playSound as it was not required to get the length.

I tried it in your way, and the value is 32026


#include "fmod.hpp"
#include "common.h"

int FMOD_Main()
{
    FMOD::System     *system;
    FMOD::Sound      *sound1;
    FMOD::Channel    *channel = 0;
    FMOD_RESULT       result;
    unsigned int      version;
    void             *extradriverdata = 0;
    
    Common_Init(&extradriverdata);

    /*
        Create a System object and initialize
    */
    result = FMOD::System_Create(&system);
    ERRCHECK(result);

    result = system->getVersion(&version);
    ERRCHECK(result);

    if (version < FMOD_VERSION)
    {
        Common_Fatal("FMOD lib version %08x doesn't match header version %08x", version, FMOD_VERSION);
    }

    result = system->init(32, FMOD_INIT_NORMAL, extradriverdata);
    ERRCHECK(result);

    FMOD_CREATESOUNDEXINFO exinfo;
    memset(&exinfo, 0, sizeof(FMOD_CREATESOUNDEXINFO));
    exinfo.cbsize = sizeof(FMOD_CREATESOUNDEXINFO);
    exinfo.suggestedsoundtype = FMOD_SOUND_TYPE_MPEG;
    exinfo.filebuffersize = 1024*128 * 2;

    system->createSound("https://cdn.jycut.com/audio/2020/2/11/14/8707139844651499793_15910.mp3",  FMOD_2D | FMOD_CREATESTREAM | FMOD_ACCURATETIME | FMOD_NONBLOCKING, &exinfo, &sound1);

    /*
        Main loop
    */
    do
    {
        Common_Update();

        result = system->update();
        ERRCHECK(result);

        {
            unsigned int ms = 0;
            unsigned int lenms = 0;
            bool         playing = 0;
            bool         paused = 0;
            int          channelsplaying = 0;

            if (channel)
            {
                FMOD::Sound *currentsound = 0;

                result = channel->isPlaying(&playing);
                if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN))
                {
                    ERRCHECK(result);
                }

                result = channel->getPaused(&paused);
                if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN))
                {
                    ERRCHECK(result);
                }

                result = channel->getPosition(&ms, FMOD_TIMEUNIT_MS);
                if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN))
                {
                    ERRCHECK(result);
                }
               
                channel->getCurrentSound(&currentsound);
                if (currentsound)
                {
                    result = currentsound->getLength(&lenms, FMOD_TIMEUNIT_MS);
                    if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN))
                    {
                        ERRCHECK(result);
                    }
                }
            }

            system->getChannelsPlaying(&channelsplaying, NULL);

            unsigned int len = 0;
            result = sound1->getLength(&len, FMOD_TIMEUNIT_MS);
            printf("%d\n",len);
            
        }

        Common_Sleep(50);
    } while (!Common_BtnPress(BTN_QUIT));

    /*
        Shut down
    */
    result = sound1->release();
    ERRCHECK(result);
    result = system->close();
    ERRCHECK(result);
    result = system->release();
    ERRCHECK(result);

    Common_Close();

    return 0;
}

Apologies, I was loading the file from disk initially not using a net stream.

With net streams we try to estimate the length using the frame size, because we don’t have the actual size available to us. Generally this in usually about 80% correct but in this case the file has a variable bit rate (VBR) and there is something odd about the first frame in the file, which is causing the calculations to be well off.

We have recorded the duration of each audio on the server. Can we set the audio length manually to solve the problem of incorrect duration?

It does not look like there is a way to manually set the length of the audio in FMOD, you would have to cache the file sizes outside of FMOD (eg. you could store them as a pair with the file path).

Thank you for your reply, :blush:
If I cache the file size outside of Fmod, what should I do next? Can you tell me in detail? It’s better to have code examples

Well it depends on what you want to do with the length, but an easy way to store could be in a pair/map with the string path used as the key.