- Home /
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("\n\n\n");
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("\n\n\n");
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
2) if frequency is 24000Hz - length is multiplied by two
Is there any way to get normal lenght ?
$$anonymous$$inda hacky, but you could just multiply the length by clip's Frequency / 44100Hz (if you can access the frequency via script).
Answer by Bunny83 · Apr 20, 2016 at 01:06 AM
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?
Hi. I develop now for iOS & android. so stream/download mp3 files is good for now.
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
but the problem is that the second track (774144 samples at 24 kHz) really has 16.128 seconds when I save the file locally - any music player says that track has 16 second length. Even if I drop mp3 file in unity's assets folder - editor in preview displays 16.128 seconds lenght. but if I load file via www - I receive double length 32.256s
Well, the only thing that's possible is, that it only detects a single channel. As you can see at the top the audio file has 1 channel. That means it's interpreted as mono audio. If it's actually a stereo audio file the time would be half the size. The actual formula for the clip length is:
length = nSampled / (freq * nChannel).
So either it's format isn't recognised correctly or the file is otherwise corrupted such as that the header contains wrong data.
As said, as far as i know Unity only supports mp3 strea$$anonymous$$g on mobile. I don't think you can actually load an mp3 to access the decompressed PC$$anonymous$$ samples. Also you usually can only play one mp3 at a time as it's played by the native (hardware) mp3 decoder on those devices.
I have never really tested this. $$anonymous$$aybe that has changed but i doubt it.
Your answer
Follow this Question
Related Questions
How to make walking sound effects? 0 Answers
3D sound crackling problem 0 Answers
How do I make enemies play a sound when they die? 1 Answer
how to make an sliding sound effect? 2 Answers