Search Unity

Audio Music loop system repeats incorrectly

Discussion in 'Audio & Video' started by Game_Builder101, Jul 30, 2019.

  1. Game_Builder101

    Game_Builder101

    Joined:
    Sep 29, 2018
    Posts:
    1
    So I have written some code for my game that is intended to loop audio tracks, be able to switch at any time to a new clip, fade between them, and be able to remember the time of specific clips. However, a reoccurring issue has been that tracks that have been replaying from random points, even when not "remembered". Could anyone help provide some insight?

    PS: This code is an atrocious catastrophe of code

    Code (CSharp):
    1. public void SetAmbience(AudioClip loop, float intermission, bool transition, float transitionSpeed = 0.0f)
    2.     {
    3.         if (_ambientSource.clip != loop)
    4.         {
    5.             if (_ambLoopCoro != null) StopCoroutine(_ambLoopCoro);
    6.  
    7.             _ambLoopCoro = StartCoroutine(IAmbience(loop, intermission, transition, transitionSpeed));
    8.         }
    9.     }
    10.  
    11.     private IEnumerator IAmbience(AudioClip loop, float intermission, bool transition, float transitionSpeed = 0.0f)
    12.     {
    13.         if (transition && _ambientSource.clip != null)
    14.         {
    15.             AmbienceFadeSilent(transitionSpeed);
    16.             WaitUntil w = new WaitUntil(() => ambFaded);
    17.             yield return w;
    18.             AmbienceFadeFull(transitionSpeed);
    19.         }
    20.  
    21.         _ambientSource.clip = loop;
    22.  
    23.         while (true)
    24.         {
    25.             _ambientSource.Stop();
    26.             _ambientSource.Play();
    27.  
    28.             AmbientMemory ambMem = _ambMemory.Find(x => x.clip == loop);
    29.             if(ambMem != null)
    30.             {
    31.                 _ambientSource.Play();
    32.                 _ambientSource.time = ambMem.time;
    33.             }
    34.  
    35.             WaitUntil wu = new WaitUntil(() => !_ambientSource.isPlaying);
    36.             yield return wu;
    37.             WaitForSeconds ws = new WaitForSeconds(intermission);
    38.             yield return ws;
    39.         }
    40.     }
    41.  
    42.     public void RemeberAmbience()
    43.     {
    44.         if (_ambientSource.clip != null)
    45.         {
    46.             AmbientMemory ambMem = _ambMemory.Find(x => x.clip == _ambientSource.clip);
    47.  
    48.             if (ambMem == null)
    49.             {
    50.                 _ambMemory.Add(new AmbientMemory(_ambientSource.clip, _ambientSource.time));
    51.             }
    52.             else
    53.             {
    54.                 ambMem.time = _ambientSource.time;
    55.             }
    56.         }
    57.     }
    58.  
    59. public class AmbientMemory
    60. {
    61.     public AudioClip clip;
    62.     public float time;
    63.  
    64.     public AmbientMemory(AudioClip target, float rTime)
    65.     {
    66.         clip = target;
    67.         time = rTime;
    68.     }
    69. }