- Home /
streaming audio clipping with unity 3.X
While using unity2.6, we had this problem where the audio sound clip would cut out mid-way through playing towards the end of the clip. After upgrading to unity3.X we noticed this continued to happen. Is there any solution / workaround with the new version of unity?
Thanks.
(better code formatting...)
private IEnumerator PlayEffect( GameObject effect ) { while( !effect.audio.clip.isReadyToPlay ) { yield return( 1 ); }
effect.audio.Play(); yield return( new WaitForSeconds( effect.audio.clip.length ) );
while( effect.audio.isPlaying ) { yield return( 1 ); } DestroyImmediate( effect.audio.clip ); DestroyImmediate( effect ); }
are you checking AudioClip.isReadyToPlay before you start playing? ( http://unity3d.com/support/documentation/ScriptReference/AudioClip-isReadyToPlay.html )
when we want to play a strea$$anonymous$$g audio clip, the following coroutine gets started...
private IEnumerator PlayEffect( GameObject effect )
{
while( !effect.audio.clip.isReadyToPlay )
{
yield return( 1 );
}
effect.audio.Play();
yield return( new WaitForSeconds( effect.audio.clip.length ) );
while( effect.audio.isPlaying )
{
yield return( 1 );
}
DestroyImmediate( effect.audio.clip );
DestroyImmediate( effect );
}
sorry for the poor formatting... as you can see, we are yielding for the audio clip's length, and then for good measure, yielding while the isPlaying flag is returning true on the audio component.
Everything I would check looks fine there to me, I now defer to stronger $$anonymous$$ds...
Answer by Flynn · Jan 17, 2011 at 10:54 AM
Everything checks out with me too. In fact, I programmed something not-to-different and it worked fine.
Perhaps the issue is in your audio file? Unity might have troubles importing it off the net.
DISCLAIMER: These .ogg URLs are provided by the Xiph Open Source Community. http://www.vorbis.com/music/Epoq-Lepidoptera.ogg <-- Recommended. Starts up fast, loud audio. http://www.vorbis.com/music/Hydrate-Kenny_Beltrey.ogg <-- Shortest one http://www.vorbis.com/music/Lumme-Badloop.ogg http://www.vorbis.com/music/Mists_of_Time-4T.ogg http://www.vorbis.com/music/The_Abyss-4T.ogg
Source: http://www.vorbis.com/music/
I hope this helps. -- Flynn
Doesn't work at all with 3.2 on Windows. Nor does the code straight from the docs: http://unity3d.com/support/documentation/ScriptReference/WWW.GetAudioClip.html
Answer by Waz · Feb 18, 2011 at 12:41 AM
Seems in 3.2 streaming does not work at all for OGG files: once you call WWW.audioClip, you've just got the samples for whatever OGG is so-far downloaded (not much when isReadyToPlay first becomes true!).
The best I've been able to do is this horrible hack (basically reconvert from OGG once the bit that was downloaded is almost finished playing):
var url = "http://www.vorbis.com/music/Epoq-Lepidoptera.ogg";
function Start () { var www = new WWW(url); audio.clip = www.audioClip; while (!www.isDone) { yield WaitForSeconds(1); if (audio.time > audio.clip.length-2) { audio.clip = www.audioClip; } } }
function Update () { if(!audio.isPlaying && audio.clip.isReadyToPlay) audio.Play(); }
Your answer
Follow this Question
Related Questions
streaming local sound files on iOS 1 Answer
Audio Artefacts When Stopping/Destroying during play. 2 Answers
Get certain audio sources from unity as buffer? 0 Answers
Streaming audio from ffmpeg RTP into Unity 0 Answers
Why does loading music files at runtime in android using www class takes a lot of time? 0 Answers