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!
Your answer
Follow this Question
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