|
I've got my audio clips being dynamically loaded by WWW, and I have pre-set an audio source on an invisible gameobject that sits right by the main camera. As I load new audio clips, I simply make a copy of the invisible gameobject, and then assign the clip to the new source. That works great, but of course I can only have each clip playing once at a time. So, if I have clip A, for instance, I can't have clip A playing over top of itself if two things trigger clip A semi-simultaneously. The code I am using for copying the gameobject of the audio source is simply this:
And that works great for the audio sources. However, if I assign the same clip to multiple audio sources, it seems that the same issue as noted above is there: it can't play on top of itself, it simply resets to the start when it is triggered the second time. So, I am therefore trying to do something similar for the Audio Clip, copying that once for each Audio Source, but when I do that it winds up with no sound playing at all:
That code doesn't throw any errors, but it also doesn't play any sound. It doesn't matter if I wait until after the initial audio clip has isReadyToPlay as true or not. Any thoughts? Surely this must be a pretty common need, with multiple guns of the same sort on a battlefield, etc. In DirectX in the past I've had to just make multiple copies of a SecondarySoundBuffer to play them independently, but that's so wasteful of memory -- my hope is to find a better approach here...
(comments are locked)
|
|
if loading with Resources, you dont need to copy the clip. If loading with www, just get the clip from the www class and it will be a copy.
(comments are locked)
|
|
Probably the easiest way is to use AudioSource.PlayClipAtPoint(), which handles the housekeeping for you. I tried that also, but it has the same problems when I pass the same clip to it two times in a row.
Mar 23 '10 at 06:44 PM
x4000
(comments are locked)
|
|
the problem is that each AudioSource is referencing the same AudioClip instance. Each time you create a new AudioSource you should also create a new AudioClip for that source (if you want multiple instances of the same sound playing). Id suggest using WWW.GetAudioClip() or WWW.audioClip since they by default return a copy (not a reference). Otherwise you can use Instantiate to create a copy of your AudioClips.
(comments are locked)
|
|
to clarify, the instantiate would look like this: there is a bug however with PlayOneShot if you make copies of your WWW generated clips with Instantiate. It will not play and returns a channel error.
Jul 27 '11 at 11:29 AM
pixelshaded
also note that you do not need to create copies of your sound if using Resources.Load
Jul 27 '11 at 11:36 AM
pixelshaded
(comments are locked)
|
|
How do you slove this question finally? I'm in a puzzle as to slove this problem too!!! Could you teach me ?
(comments are locked)
|
