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
0
Question by Kruai · Feb 08, 2014 at 12:02 AM · audio

How do I gaplessly change music clips in a script?

I'm working on a project that is going to have dynamic music. I have all my music clips in an array, and I switch to another clip whenever I want the music to vary.

Right now, I just play one clip, and in the update loop, I check if the audio is playing. If audio.isPlaying is false, I tell it to play the next clip. However, this makes a small gap in the audio that isn't present if I simply "loop" one clip. This is distracting and stifles the energy of the song.

How can I eliminate this gap?

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 getyour411 · Feb 08, 2014 at 12:01 AM 0
Share

Could you rampDown volume of ClipX while transitioning/rampUP volume of ClipY

avatar image Kruai · Feb 08, 2014 at 12:03 AM 0
Share

An AudioSource can only play one clip at a time though, right? How would I have to clips playing at once?

avatar image getyour411 · Feb 08, 2014 at 12:07 AM 0
Share

No, you can play multiple audio clips at the same time. Example here http://answers.unity3d.com/questions/52017/2-audio-sources-on-a-game-object-how-use-script-to.html

http://answers.unity3d.com/questions/175995/can-i-play-multiple-audiosources-from-one-gameobje.html

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by rejwan1 · Feb 08, 2014 at 01:02 AM

We solved this issue by writing a cross-fading script. Think of it as a DJ cross-fades two songs together - he lowers the volume of the currently playing song while raising the volume of the 2nd song.

A simple "value to tween" can be used to crossfade easily with two audio sources - which luckily for you, iTween supports (http://itween.pixelplacement.com/documentation.php#ValueTo)

Pseudo code for CrossFader.cs :

 AudioSource first, second;
 
 private void CrossFade(AudioClip fadeTo)
 {
 // create a hashtable with from 0 to 1, with a 1 second time, calling CrossFade
 iTween.ValueTo(gameObject, hashTable);
 second.Play(fadeTo);
 }
 
 private void CrossFade(float currentValue)
 {
 first.volume = 1 - currentValue;
 second.volume = currentValue;
 }
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 thekingofclubs · Aug 21, 2014 at 11:05 PM

I played around with this issue too and after trying all kinds of things the best result I got was using FixedUpdate. I realized that the gap occurred because Update is called only once per frame and that meant the new track loaded at the beginning of the new frame. So until the new frame loaded there was a slight but noticeable gap. FixedUpdate is called multiple times per frame, not always, but most of the times. This code might help:

 var bgMusicArray : AudioSource[]; //Add audiosources in the inspector
 private var bgMusic : AudioSource;
 var playNext : boolean = false;

 function FixedUpdate()
 {
     if(!bgMusic.isPlaying)
         {
             playNext = true;
         }
         if(playNext)
             ChangeMusic(2);
 }
 
 function ChangeMusic(musicNumber : int)
 {
     if(playNext)
     {
         playNext = false;
         bgMusic = bgMusicArray[musicNumber];
         bgMusic.Play();
     }
 }

The result is not completely satisfactory, I will keep working on this but for now it suffices.

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 Eric5h5 · Aug 21, 2014 at 11:06 PM 0
Share

You shouldn't use FixedUpdate; it's only for physics, and typically is called less often than Update since the default is 50fps.

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

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

Related Questions

How to play several audio cllips one after another. 3 Answers

Slowly fade audio's pitch on key press? 3 Answers

Play audio on keyboard click? 1 Answer

How does compressing Looping Wav Files work? 0 Answers

one shot Sound on collision. Unity 4.0 problem. 2 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