AudioClip calculates length wrong

Hi everyone !

When I’m loading clips from web I’ve got this issue.

 private IEnumerator WaitAudioclipLoad(string shortCode, string url)
        {
            WWW ww = new WWW(url);
            yield return ww;
            if (ww.error != null)
            {
                Debug.Log("ErrorOccured :" + ww.error);
                OnAudioclipLoaded(shortCode, null);
            }
            else
            {
                var audioClip = ww.GetAudioClip(false, false, AudioType.MPEG);
                audioClip.LoadAudioData();

                File.WriteAllBytes(Application.dataPath + "/" + shortCode + ".mp3", ww.bytes);
                Debug.Log("

");
Debug.Log("channels " + audioClip.channels);
Debug.Log("frequency " + audioClip.frequency);
Debug.Log("length " + audioClip.length);
Debug.Log("loadState " + audioClip.loadState);
Debug.Log("loadType " + audioClip.loadType);
Debug.Log("preloadAudioData " + audioClip.preloadAudioData);
Debug.Log("samples " + audioClip.samples);

                Debug.Log("

");
CacheProvider.CacheProvider.Instance.AddToCache(shortCode, ww.bytes, audioClip);
OnAudioclipLoaded(shortCode, audioClip);
}
ww.Dispose();
}

i load the clip via www ( and wait til the clip is loaded). ater that I save the clip locally ( exaple is for editor) and dump AC data to console.

and some strange things I sow in console:

  1. if frequency is 44100Hz - length is normal
    68374-screen-shot-2016-04-19-at-171603.png

  2. if frequency is 24000Hz - length is multiplied by two
    68373-screen-shot-2016-04-19-at-171618.png

Is there any way to get normal lenght ?

I don’t see any error here. Your first clip has 662400 samples at 44.1 kHz and your second clip has 774144 samples at 24 kHz. Just calculate it yourself:

662400 / 44100 == 15.02040 seconds

774144 / 24000 == 32.256 seconds

Also keep in mind that mp3 can only be used on Android / iOS and maybe on windows phone as well. On all other platforms you can’t dynamically load mp3s. See the documentation for more details. I’m also not sure if you can actually access any data of mp3s on Android as Unity just uses the hardware player to play mp3. Have you actually tried playing those clips? Also for which platform do you develop?