- Home /
www.getAudioClip() Bug: One Second being cut off MP3.
Disclaimer: There are a few of these posts lingering around but nothing concrete enough for me or my company to call definite. Most of the posts are regarding streaming audio from a server, here I am trying to do a full download
Problem: Urgent!
WWW.getAudioClip(false, false) is returning a playable mp3 from our servers with the wrong length. One second is being removed from each of our mp3's. When going directly through our RESTFUL resource ( going to the url of the mp3 in a web browser ) the mp3 plays at full length. This is a major issue as we are sending voice messages between users.
Here's an example of what we have tried. We are not looking to stream the audio, hence the second false, but rather do a full download before playing.
public IEnumerator StartAudio(string url)
{
AudioClip clip;
WWW www = new WWW (url);
clip = www.GetAudioClip(false, false);
while(www.progress < 1.0f && !www.audioClip.isReadyToPlay){ yield return null; }
audioSrc.clip = clip;
audioSrc.Play();
if(isWaitingforAudio){ StopAllCoroutines(); }
StartCoroutine(WaitForAudioFinish( audio.clip.length ));
www.Dispose();
}
I cannot give our URLs or any resources due to them being property of a different party. If you require any more specific information let me know.
This has the same results:
public IEnumerator StartAudio(string url)
{
AudioClip clip;
WWW www = new WWW (url);
yield return www;
clip = www.GetAudioClip(false, false);
audioSrc.clip = clip;
audioSrc.Play();
if(isWaitingforAudio){ StopAllCoroutines(); }
StartCoroutine(WaitForAudioFinish( audio.clip.length ));
www.Dispose();
}
How do you know that too few bytes have been fetched? Does WWW.bytesDownloaded
consistently give you too few bytes? Do the AudioClip.length
, samples
and frequency
look correct? I wonder if the playback is too slow and the length kicks in before the samples are used up. Could mean the frequency is wrong?
Answer by CostelloNicho · Mar 25, 2014 at 07:44 PM
For anyone curious I switched over to streaming and this solved my problem. I will not be marking this as the answer because it does not directly address the question, its more of how I worked around it.
public IEnumerator StartAudio(string url)
{
www = new WWW (url); // Make the www request
while(!www.audioClip.isReadyToPlay) // Wait till we have enough of a buffer to play the audio consitently
yield return null;
audioSrc.clip = www.GetAudioClip(false, true, AudioType.MPEG);
audioSrc.loop = false;
audioSrc.Play();
if(isWaitingforAudio){ StopAllCoroutines( ); }
StartCoroutine(WaitForAudioFinish( audio.clip.length ));
}
There are a few things I found important here:
Do not dispose of www instance while streaming. I only call dispose if the object is destroyed.
Do not assigned getAudioClip() to a cached local variable, this had some unexpected results.
Assigned the audio source directly with the stream ( www.getAudioClip() )
No need to wait for a specific progress (www.progress), just wait till the www.audioClipIsReady before you assigned the audio clip stream to the audio source.
After further experimentation I've realized the above doesn't work as it is still downloading the full audio clip. The current not perfect solution I'm using is this:
/// <summary>
/// Start Audio:
/// - Downloads an audio clip from the server and plays.
/// </summary>
/// <param name="url">URL of the audio clip.</param>
public IEnumerator StartAudio(string url)
{
sprite.color = loadingColor;
//Set the color of the audio icon
www = new WWW (url); // Make the www request
while(www.progress < .15f)
{
yield return null;
}// Wait till we have enough of a buffer to play the audio consitently
audioSrc.clip = www.GetAudioClip(false, true, AudioType.MPEG);
audioSrc.loop = false;
if(!audioSrc.clip.isReadyToPlay)
{
yield return null;
}
audioSrc.Play();
}
Notes:
Audio some times (mainly with large files ) only plays the first 3 seconds then cuts off for the rest of the duration. When having that audio source play again from what was cached from the last request it play the full audio.
A small amount of of the mp3 file is being cut off the front and back of the audio file.
Still looking for more input. Thanks for your time.
Thank you for your workaround. After converting a project from Unity 4 to 5, I started getting this problem with audio clips loaded from Strea$$anonymous$$gAssets getting cut off, thankfully the GetAudioClip with strea$$anonymous$$g set to true seems to work well enough.
Answer by Fattie · Jan 21, 2016 at 08:47 PM
Has Unity ever bothered even to address this problem?
Two years later ..
Your answer
Follow this Question
Related Questions
How to have www streaming to work with a radio station? 1 Answer
WWW GetAudioClip not working correctly for Unity 4? 1 Answer
loading/streaming audioclip using www class [android] 0 Answers
How do i stream music from a server? 0 Answers
How to avoid lag when using WWW class to download sound 3 Answers