• 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
1
Question by Tatanan · Jan 05, 2015 at 09:06 PM · soundmp3wav

.wav or .mp3 for audioclips?

I know there are already some questions about t$$anonymous$$s points in t$$anonymous$$s forum, but I can't find a good complete response. Basically, I have several sounds for my games, and I have the sound in any format (wav, mp3...), so w$$anonymous$$ch one should I use?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
10
Best Answer

Answer by MakinStuffLookGood · Jan 05, 2015 at 09:35 PM

Typically WAVs are used for small sounds files (sound effects) and MP3s are used for longer, streamed files (background music).

Reasoning is that the WAVs will be stored in memory and there's little runtime overhead to decode them when you want play them (bullet sfx that 100s of times throughout the game). MP3s for a a background song on the other hand could be 3 MB, and the uncompressed wav file of that same file could be >10 MB. Not somet$$anonymous$$ng you want sitting around in memory, so it's better to leave them compressed, and use stream from disc. Little overhead when you're just doing t$$anonymous$$s for one music track.

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 Tatanan · Jan 06, 2015 at 12:06 AM 0
Share

Perfect answer, thank you. I can't still vote, but take my +1.

avatar image Max_Bol · Jan 04, 2021 at 08:27 PM 0
Share

There's one slight little addition I would add onto this answer.

If you're either reading or editing the audio clip/sound in the game, it's highly advisable to use the WAV format and not the MP3 format.

For example, when making a game where a music affects parts of the world, you should use WAV format instead of MP3 even if it's for a music. Otherwise, when using MP3, Unity will create an uncompressed clone of the music data to read the audio outputs in it. When using the WAV format, Unity doesn't create a clone of the data and, instead, use the RAM stored data of the file. (This is only happening when you try to read data out of the MP3 file.) An common/well known example of this is if you have some lights show or enemies spawning based on the music's tones.

The same should be taken in consideration if you affect the music output in-game. For example, if you change the tones or style or speed of the music in-game (like toning down the music in an horror game when danger is close), you should also use WAV instead of MP3 for the music. As soon as you manipulate the output of any bits in the MP3, Unity stores it as RAW audio data while, with the WAV, Unity simply modify the RAM stored data of the WAV file itself.

Those are specific conditions, but I think they are important to specify never the less.

avatar image
4

Answer by Kamuiyshirou · Jan 05, 2015 at 09:43 PM

1 - Any Audio File imported into Unity is available from scripts as an Audio Clip instance, w$$anonymous$$ch is effectively just a container for the audio data. The clips must be used in conjunction with Audio Sources and an Audio Listener in order to actually generate sound. When you attach your clip to an object in the game, it adds an Audio Source component to the object, w$$anonymous$$ch has Volume, Pitch and a numerous other properties. W$$anonymous$$le a Source is playing, an Audio Listener can “hear” all sources wit$$anonymous$$n range, and the combination of those sources gives the sound that will actually be heard through the speakers. There can be only one Audio Listener in your scene, and t$$anonymous$$s is usually attached to the Main Camera.

Supported Formats

alt text

Font: http://docs.unity3d.com/Manual/AudioFiles.html

2 - Create one Sound Controller:

 using UnityEngine;
 using System.Collections;
 
 public enum soundsGame{
     die,
     toque,
     menu,
     point,
     wing
 }
 
 public class SoundController : MonoBehaviour {
 
     public AudioClip soundDie;
     public AudioClip soundToque;
     public AudioClip soundMenu;
     public AudioClip soundPoint;
     public AudioClip soundWing;
 
     public static SoundController instance;
 
     // Use t$$anonymous$$s for initialization
     void Start () {
         instance = t$$anonymous$$s;
     
     }
     
     public static void PlaySound(soundsGame currentSound){
         switch(currentSound){
         case soundsGame.die:{
             instance.audio.PlayOneShot(instance.soundDie);
         }
             break;
         case soundsGame.toque:{
             instance.audio.PlayOneShot(instance.soundToque);}
             break;
         case soundsGame.menu:{
             instance.audio.PlayOneShot(instance.soundMenu);
         }
             break;
         case soundsGame.point:{
             instance.audio.PlayOneShot(instance.soundPoint);
         }
             break;
         case soundsGame.wing:{
             instance.audio.PlayOneShot(instance.soundWing);
         }
             break;
         }
     }
 }

3 - Create one GameObject(example: cube).

4 - Insert component audio source.

5 - Drag class SoundController to GameObject.

6 - Drag audio(.mp3 or .wav) to Script GameObject(Example: Sound toque).


format-audio.png (16.9 kB)
Comment
Add comment · Show 4 · 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 MakinStuffLookGood · Jan 05, 2015 at 09:51 PM 1
Share

That enum and switch case thing seems a bit ridiculous... you could just as easily use a Dictionary that uses the name of the clip as the key with the clip as the value. Then you wouldn't need an additional variable, enum declaration, and switch case for every new sound you add.

avatar image DavidSWu MakinStuffLookGood · Feb 09, 2019 at 08:13 PM 0
Share

True, but dictionaries have a lot of overhead. Obviously, for a real project, you would not use a switch statement, but in most cases, a simple List<(Key,V)> (<=64 items) or SortedList (<=1024 items) are a better choice. This is less relevant if you don't care about memory consumption or garbage. Caveat: SortedList is much worse than List for lookups and insertion primarily due to poor cache coherency and difficult to predict branching.

avatar image RafaelMariano · Jan 05, 2015 at 09:51 PM 0
Share

With that I can play audio ! Thanks!

avatar image Tatanan · Jan 06, 2015 at 12:06 AM 0
Share

Thank you for the answer. I've read it and learn from it, althought it was a bit weird as answer, since it seems you are answering to another question, but still I learnt.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

MP3 and WAVs suddenly corrupt 1 Answer

Is it efficient to use .mp3, rather than using .wav? 3 Answers

Press Button for Sound 1 Answer

MP3 corruption 2 Answers

Sound not playing on Galaxy S4 device 0 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