• 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
0
Question by Liens · Feb 22, 2013 at 08:19 AM · audioaudiosourceloopaudioclip

Audio loops too early

I have music (mp3's) from an array being played as 2D AudioClips in an AudioSource. For some reason the music plays for a while then loops when the song is in the last quarter of playthrough and not even finished playing yet. Does anyone know how to resolve this issue?

Here's some code attached to the AudioSource playing the clips.

 #pragma strict
 
 enum MusicTracks { Sea, Land }
 
 var music : AudioClip[];
 
 function OnLevelWasLoaded () {
     switch (Application.loadedLevelName) {
         case "Land":
             var newAudioClip : AudioClip = music[MusicTracks.Land];
             if(audio.clip != newAudioClip) {
                 audio.clip = newAudioClip;
                 audio.Play();
             }
         break;
         case "Sea": case "Sea_Under":
             newAudioClip = music[MusicTracks.Sea];
             if(audio.clip != newAudioClip) {
                 audio.clip = newAudioClip;
                 audio.Play();
             }
         break;
     }
 }
Comment
Add comment · Show 3
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 GC1983 · Feb 22, 2013 at 08:54 AM 1
Share

Tough to say. Switch statements are screwy. Good practice I learned was add in a Debug.Log one by one on each line between code to see when it was being executed. That might tell you if its being cut off at a certain point by something else or it could be your mp3 cutting off.

avatar image barinelg · Feb 22, 2013 at 06:25 PM 0
Share

Agreed with GC1983. Put in a Debug statement on both and keep an eye out for it. I believe your goal is for this function to only be called once, so I'm curious to see if it is in fact being called twice.

avatar image Liens · Feb 24, 2013 at 08:29 AM 0
Share

I used Debug.Log and the code is working as intended, there isn't anything wrong with the clip itself either. I wonder if it could be to do with the "Load Type" setting?

2 Replies

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

Answer by Liens · Feb 24, 2013 at 08:33 AM

The MP3's import settings, "Load Type" was set to "Compressed in memory", once I set it to "Decompress on load" this did not happen any longer. I am unsure of why this makes a difference.

Edit: After reading the documentation, "Decompress on load" is not reccomended for large files, so I switched it to "Stream from disc". It also seems to solve this problem.

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
avatar image
0

Answer by David Kamunyu · Feb 22, 2013 at 10:29 AM

Hey,

From previous experience, I have learnt that it is a good idea to avoid case and while. Don't get me wrong, I love swearing into the screen as much as the next guy when things aren't working correctly, but IF has been a long time friend and companion and would always work well, so long as the checks are good.

So if you could make a check, like if (land){CODE} else if (sea || Sea_Under){code} and so on, you can even toss in a check that can ensure that the music plays only at a certain time... maybe by calling a function for music playing in {code part}, with an audio.loop = true; line. So one call and the rest you leave to the system.

I don't want to change your code, but it could save you a headache :)

However, if you have some kind of superpowers and figure out how to make the CASE work, please x 20 share, because I'm also interested in how to make use of it... :D

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 GC1983 · Feb 22, 2013 at 05:34 PM 0
Share

Switch statements and if statements pretty much do the same thing.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

12 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

Related Questions

Is there a way to create a random Audiosource loop? 2 Answers

Question about audio (AudioSource). My ingame sound doesn't sound like the original audio file? 3 Answers

Can't seem to play my AudioClip? 2 Answers

Clicking/popping sound when playing audio clip 0 Answers

Changing pitch of different clips from one AudioSource 1 Answer


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