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 /
  • Help Room /
avatar image
0
Question by ElChileVengador · May 18, 2021 at 03:00 PM · audioaudiosourceaudioclipvolume

AudioSource not responding to volume change

Hi Unity forum!

I'm having a really weird issue with my Audio Sources and I'm wondering if anyone can help. I have a method that causes a crossfade between two audio sources. Basically it's an Ienumerator that lerps one volume to 0 and the other to a max. However, sometimes the old source does not dip in volume at all.


This happens inconsistently and I haven't been able to determine a pattern. There are no errors in the inspector and when I fill it with logs, I see the correct values being assigned to the source. As a matter of fact, I stopped the game right after seeing "oldSource.volume = 0" in the log, only to see it still be the max value in the inspector.

I've added logs as well to other spots where the volume is changed and none of them trigger.


here's the crossfade code:

 IEnumerator CrossFadeMusic(KeyValuePair<V4_GameSound, AudioSource> _oldTrack, KeyValuePair<V4_GameSound, AudioSource> _newTrack, float _timeToFade, bool _stopPreviousMusic)
     {
         V4_Debug.instance.Logger($"<color=yellow>AM- Cross fade START: old track {_oldTrack.Key.name} vol: {_oldTrack.Value.volume}  new track volume: {_newTrack.Key.name} vol: {_newTrack.Value.volume}</color>", 1 << 2);
 
         float _timer = 0.0f;
         // get current volume of track
         var _oldMusicStartVolume = _oldTrack.Key.volume;
         // set target of new track
         adaptiveAudioVolumeHolder = _newTrack.Key.volumeReference;
 
         // start the new track at 0
         _newTrack.Value.volume = 0;
 
         if (!_newTrack.Value.isPlaying)
             _newTrack.Value.Play();
         V4_Debug.instance.Logger($"<color=yellow>AM- is new track playing now? {_newTrack.Value.isPlaying}</color>", 1 << 2);
 
         while (_timer <= _timeToFade)
         {
             // new track fade up
             _newTrack.Value.volume = Mathf.Clamp01(Mathf.Lerp(0, adaptiveAudioVolumeHolder, _timer / _timeToFade)) * volume_master / 100 * volume_music / 100;
             // old track fade down
             _oldTrack.Value.volume = Mathf.Clamp01(Mathf.Lerp(_oldMusicStartVolume, 0, _timer / _timeToFade)) * volume_master / 100 * volume_music / 100;
 
             yield return new WaitForEndOfFrame();
             _timer += Time.deltaTime;
         }
 
         // if it's not the same track
         V4_Debug.instance.Logger($"<color=yellow>AM- oldtrack volume {_oldTrack.Value.volume}</color>", 1 << 2);
         V4_Debug.instance.Logger($"<color=yellow>AM- same track? {_oldTrack.Value == _newTrack.Value}</color>", 1 << 2);
         if (_oldTrack.Value != _newTrack.Value)
         {
             // make sure the old track is 0
             _oldTrack.Value.volume = 0f;
 
             // completely stop old track
             V4_Debug.instance.Logger($"<color=yellow>AM- stop prev track {_stopPreviousMusic}</color>", 1 << 2);
             // you can't stop a track if you're the same one
             if (_stopPreviousMusic)
             {
                 Stop(_oldTrack.Key.name);
                 V4_Debug.instance.Logger($"<color=yellow>AM- stop prev track {_oldTrack.Key.name} - {_oldTrack.Key.source.clip.name}</color>", 1 << 2);
 
                 yield return new WaitForEndOfFrame();
                 // reset old music volume
                 _oldTrack.Value.volume = _oldTrack.Key.volumeReference * volume_master / 100 * volume_music / 100;
             }
         }
 
         yield return new WaitForEndOfFrame();
         // reset volumes in sources to original values
         _newTrack.Value.volume = adaptiveAudioVolumeHolder * volume_master / 100 * volume_music / 100;
 
         // store new track as current one
         currentGameMusic = _newTrack;
 
         V4_Debug.instance.Logger($"<color=yellow>AM- Cross fade END: old track {_oldTrack.Key.name}({_oldTrack.Value.clip}) vol: {_oldTrack.Value.volume}  new track volume: {_newTrack.Key.name} vol: {_newTrack.Value.volume}</color>", 1 << 2);
     }

Any direction or other avenues for me to investigate would be greatly appreciated. I've been hunting this bug down for days.

Cheers!

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

0 Replies

· Add your reply
  • Sort: 

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

194 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 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 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 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 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 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 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 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 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 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

Pick Up Sound 0 Answers

Trying to use derive pitch and db from realtime microphone input 0 Answers

Audio Cutting Out Unexplainably 1 Answer

Audio not playing when conditions are met. 0 Answers

Why won't my second Audio Source play? 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