x


What is the proper way to copy an audio clip?

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

more ▼

asked Mar 23 '10 at 04:49 PM

x4000 gravatar image

x4000
504 23 25 40

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

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.

more ▼

answered Feb 10 '12 at 09:34 PM

pixelshaded gravatar image

pixelshaded
1 1 1 2

(comments are locked)
10|3000 characters needed characters left

Probably the easiest way is to use AudioSource.PlayClipAtPoint(), which handles the housekeeping for you.

more ▼

answered Mar 23 '10 at 05:51 PM

Eric5h5 gravatar image

Eric5h5
80.1k 41 132 519

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)
10|3000 characters needed characters left

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.

more ▼

answered Jul 22 '11 at 01:00 PM

pixelshaded gravatar image

pixelshaded
1 1 1 2

(comments are locked)
10|3000 characters needed characters left

to clarify, the instantiate would look like this:

AudioClip clip = www.audioClip;
AudioClip clip2 = (AudioClip)Instantiate(clip);
more ▼

answered Jul 22 '11 at 01:02 PM

pixelshaded gravatar image

pixelshaded
1 1 1 2

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)
10|3000 characters needed characters left

How do you slove this question finally?

I'm in a puzzle as to slove this problem too!!!

Could you teach me ?

more ▼

answered Feb 10 '12 at 07:16 AM

sark12500 gravatar image

sark12500
-9 3 6 7

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1024
x169

asked: Mar 23 '10 at 04:49 PM

Seen: 1789 times

Last Updated: Feb 10 '12 at 09:34 PM