# Mp3 file use Fmod :: sound getlength return incorrect duration

**URL:** <https://qa.fmod.com/t/mp3-file-use-fmod-sound-getlength-return-incorrect-duration/15471>\
**Category:** FMOD Engine\
**Created:** [February 18, 2020, 2:04pm UTC](https://qa.fmod.com/t/mp3-file-use-fmod-sound-getlength-return-incorrect-duration/15471 "2020-02-18T14:04:35Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![donbe](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/donbe/32/965_2.png) [@donbe](https://qa.fmod.com/u/donbe)\
**Post date:** [February 18, 2020, 2:04pm UTC](https://qa.fmod.com/t/mp3-file-use-fmod-sound-getlength-return-incorrect-duration/15471/1 "2020-02-18T14:04:35Z")

</div>

I am creating my sounds like this:

```auto
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](https://cdn.jycut.com/audio/2020/2/11/14/8707139844651499793_15910.mp3)

---

<div class="post-metadata">

**Author:** ![donbe](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/donbe/32/965_2.png) [@donbe](https://qa.fmod.com/u/donbe)\
**Post date:** [February 21, 2020, 2:47am UTC](https://qa.fmod.com/t/mp3-file-use-fmod-sound-getlength-return-incorrect-duration/15471/2 "2020-02-21T02:47:06Z")

</div>

Asking for help

---

<div class="post-metadata">

**Author:** ![cameron-fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/cameron-fmod/32/261_2.png) [@cameron-fmod](https://qa.fmod.com/u/cameron-fmod)\
**Post date:** [February 26, 2020, 5:33am UTC](https://qa.fmod.com/t/mp3-file-use-fmod-sound-getlength-return-incorrect-duration/15471/3 "2020-02-26T05:33:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![donbe](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/donbe/32/965_2.png) [@donbe](https://qa.fmod.com/u/donbe)\
**Post date:** [February 28, 2020, 3:40am UTC](https://qa.fmod.com/t/mp3-file-use-fmod-sound-getlength-return-incorrect-duration/15471/4 "2020-02-28T03:40:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cameron-fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/cameron-fmod/32/261_2.png) [@cameron-fmod](https://qa.fmod.com/u/cameron-fmod)\
**Post date:** [February 28, 2020, 4:49am UTC](https://qa.fmod.com/t/mp3-file-use-fmod-sound-getlength-return-incorrect-duration/15471/5 "2020-02-28T04:49:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![donbe](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/donbe/32/965_2.png) [@donbe](https://qa.fmod.com/u/donbe)\
**Post date:** [February 28, 2020, 7:15am UTC](https://qa.fmod.com/t/mp3-file-use-fmod-sound-getlength-return-incorrect-duration/15471/7 "2020-02-28T07:15:53Z")

</div>

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

```auto

#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;
}

```

---

<div class="post-metadata">

**Author:** ![cameron-fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/cameron-fmod/32/261_2.png) [@cameron-fmod](https://qa.fmod.com/u/cameron-fmod)\
**Post date:** [March 3, 2020, 1:01am UTC](https://qa.fmod.com/t/mp3-file-use-fmod-sound-getlength-return-incorrect-duration/15471/8 "2020-03-03T01:01:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![donbe](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/donbe/32/965_2.png) [@donbe](https://qa.fmod.com/u/donbe)\
**Post date:** [March 3, 2020, 1:38am UTC](https://qa.fmod.com/t/mp3-file-use-fmod-sound-getlength-return-incorrect-duration/15471/9 "2020-03-03T01:38:13Z")

</div>

> [@cameron-fmod](#):
>
> 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？

---

<div class="post-metadata">

**Author:** ![cameron-fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/cameron-fmod/32/261_2.png) [@cameron-fmod](https://qa.fmod.com/u/cameron-fmod)\
**Post date:** [March 4, 2020, 5:46am UTC](https://qa.fmod.com/t/mp3-file-use-fmod-sound-getlength-return-incorrect-duration/15471/10 "2020-03-04T05:46:13Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![donbe](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/donbe/32/965_2.png) [@donbe](https://qa.fmod.com/u/donbe)\
**Post date:** [March 5, 2020, 1:30am UTC](https://qa.fmod.com/t/mp3-file-use-fmod-sound-getlength-return-incorrect-duration/15471/11 "2020-03-05T01:30:18Z")

</div>

Thank you for your reply, 😊  
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

---

<div class="post-metadata">

**Author:** ![cameron-fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/cameron-fmod/32/261_2.png) [@cameron-fmod](https://qa.fmod.com/u/cameron-fmod)\
**Post date:** [March 9, 2020, 10:29pm UTC](https://qa.fmod.com/t/mp3-file-use-fmod-sound-getlength-return-incorrect-duration/15471/12 "2020-03-09T22:29:53Z")

</div>

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.
