Quantcast
Channel: Latest Questions by x4000
Viewing all articles
Browse latest Browse all 49

What is the proper way to copy an audio clip?

$
0
0

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:

(GameObject)GameObject.Instantiate( Game.Instance.audioObj )

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:

(AudioClip)AudioClip.Instantiate( this.Clip );

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...


Viewing all articles
Browse latest Browse all 49

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>