- Home /
load audio files with Resources.Load and play them
I cant get it to work. I want to load three audio files at the beginning of a script and play them later responding to mouse clicks or touches. But I don't get it to work!
I fiddled around with AudioSource and AudioClip but nothing actually works. It seems that I always have to connect the audio source to a Object.
testcode:
//Audios
var aReleaseCube:AudioClip;
var aLiftCube:AudioClip;
var aCubeOnSlot:AudioClip;
function StartMainScript()
{
aLiftCube = Resources.Load("sounds/cube_up",AudioClip);
aReleaseCube = Resources.Load("sounds/release_cube",AudioClip);
aCubeOnSlot = Resources.Load("sounds/cube_on_slot",AudioClip);
aCubeOnSlot.Play();
//define some Levels
LoadLevel(currentLevel);
}
Answer by Archony · May 10, 2015 at 02:59 PM
There is no Play() function on audio clips; only on AudioSource. You might think "I don't want all that crap!", but Unity forces you to use an AudioSource anyway. I use a source created as a component on my Player GameObject.
First, load the audio clips:
AudioClip clip1 = (AudioClip) Resources.Load("Sounds/cube_release");
AudioClip clip2 = Resources.Load<AudioClip>("Sounds/cube_up");
AudioClip clip3 = Resources.Load("Sounds/cube_onslot", typeof(AudioClip)) as AudioClip;
Note that I'm using slightly different code to load each of the three clips. All of those formats will work; they all do the same thing. You can use whichever one you like best. There's JS equivalents for each of these methods.
Next, you need an AudioSource. I normally attach this to a GameObject somewhere, using the editor. You don't need three audio sources. And, for correct 3D positioning (whether you just want left/right balance or more sophisticated 3D positioning), it's easier to just set up one AudioSource and set its properties in the editor. Yes, you could instantiate it using a script as well. Your choice.
Finally, to play the sounds, use the PlayOneShot function.
audioSource.PlayOneShot(clip1);
This way, you can play a wide array of clips through one audio source.
Thanks for sharing the multiple syntax options to load from Resources, this is useful!
Answer by Eruure · Sep 09, 2013 at 02:45 AM
You might be using Resources.Load incorrectly, if I read the manual right from http://docs.unity3d.com/Documentation/ScriptReference/Resources.Load.html , it should be one of
aCubeOnSlot = Resources.Load("sounds/cube_on_slot",typeof(AudioClip));
aCubeOnSlot = Resources.Load("sounds/cube_on_slot") as AudioClip);
Then, you need to have a source, but you said it yourself.
And then it seems there might be an issue with Resources being auto-loaded as 3D clips, see this post for info:
aCubeOnSlot = Resources.Load("sounds/cube_on_slot") as AudioClip); Can't be correct ")"
AudioClip _audio = Resources.Load<AudioClip>("Sounds/whatever_audio_fx");This is valid on C#. It's called generic method, so you don't need to typecast the AudioClip too often.
Answer by DuFFOwNz · Feb 11, 2014 at 07:44 AM
If I understand correctly, you want to be able to play any of these audio clips at the same time. For that you'll need 3 separate audio sources. Write a script that does this:
audio.clip = aCubeOnSlot;
audio.Play();
and attach it to 3 separate game objects with audio sources. Then put it inside of a public function that you can call from another script.
I believe .PlayOneShot() will play any number of audio clips on one source.
Answer by Powzone · Oct 09, 2016 at 05:57 PM
Previous answers are correct but remember to put your audio files inside Resources folder!
Audio file:
Assets/Resources/Sounds/card.mp3
Code:
AudioSource sndSource = gameObject.AddComponent <AudioSource>() as AudioSource;
AudioClip sndClick = Resources.Load<AudioClip>("Sounds/card");
sndSource.PlayOneShot (sndClick);
In case anyone wants to know
sndSource.loop = true;
sndSource.PlayOneShot (sndClick);
Answer by Paulius-Liekis · Aug 29, 2012 at 01:17 PM
A couple pointers (although I'm not sure if they will solve your problem):
You have to put your audio files into StreamingAssets folder, IIRC
You have to specify full path (i.e. with extension) when loading them
Your answer

Follow this Question
Related Questions
Why does the audio sound different than the original ? 0 Answers
Adding sound javascript in unity2d 1 Answer
Sound wont play when triggered (javascript) 1 Answer
JavaScript Play Audiosource Problem 1 Answer
Question about audio (AudioSource). My ingame sound doesn't sound like the original audio file? 3 Answers