Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by fschaar · Aug 29, 2012 at 09:41 AM · javascriptaudiosourceaudioclip

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);
     
     
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

7 Replies

· Add your reply
  • Sort: 
avatar image
10

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JPhilipp · Jun 05, 2019 at 01:25 PM 0
Share

Thanks for sharing the multiple syntax options to load from Resources, this is useful!

avatar image
2

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:

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlanMattano · Jul 04, 2016 at 04:19 AM 0
Share

aCubeOnSlot = Resources.Load("sounds/cube_on_slot") as AudioClip); Can't be correct ")"

avatar image HDX13 · Aug 11, 2017 at 07:49 AM 2
Share

    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.

avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MaxxRafen · Jul 23, 2019 at 04:06 PM 0
Share

I believe .PlayOneShot() will play any number of audio clips on one source.

avatar image
1

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);
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tjbarrott · Sep 06, 2021 at 08:51 PM 0
Share

What about playing on a loop?

avatar image tjbarrott tjbarrott · Sep 06, 2021 at 11:31 PM 0
Share

In case anyone wants to know

 sndSource.loop = true;
 sndSource.PlayOneShot (sndClick);
avatar image
0

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):

  1. You have to put your audio files into StreamingAssets folder, IIRC

  2. You have to specify full path (i.e. with extension) when loading them

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges