Search Unity

Audio looping not consistent?

Discussion in 'Scripting' started by Denisowator, Jun 9, 2017.

  1. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    I want an audio to loop while a condition is true, but I can't get it to loop smoothly, either it overlaps itself (which I solved), or there are gaps between the audio or it starts playing again just before it finished (overlapping itself slightly).

    This is my setup:
    Code (CSharp):
    1.     public AudioSource audioSource;
    2.     public AudioClip sound1;
    3.  
    4.     void Update () {
    5.         if (boolean1 == true) {
    6.             if (renderer1.isVisible) {
    7.                 if (!audioSource.isPlaying) {
    8.                     audioSource.PlayOneShot (sound1);
    9.                 }
    10.             }
    11.         }
    12.     }
    Without the "if audioSource.isPlaying" it will overlap itself instantly, and sound like a buzzing noise. But currently as the audio stops, there are irregular gaps in-between when it plays next (irregular in their length of time).

    How do I fix this?

    P.S. In the inspector I have "Loop" unchecked. If I check it, nothing changes, and if I remove the "audioSource.isPLaying" statement while it's checked, it begins overlapping itself.
     
    Last edited: Jun 9, 2017
  2. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    I'm wondering if you should be using Play() instead of PlayOneShot() here, for starters. I'm honestly not sure.

    I think PlayOneShot prevents the loop from happening (because audioSource.Stop won't actually stop a oneshot, I believe). But I'm wondering if maybe you just use Play, and have the Loop flag set, and then just Stop if it's playing when the bool is false? Or if you need it to finish it's last loop, I'm assuming you can just set the audioSource Loop property to false.

    Anyways, my thoughts here are that if you let Unity handle the looping, it might be smoother. Also, obvious statement here, but ensure there's no gaps at the start/end of your audio clip
     
  3. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    I thought of that, but when I do use "Play" it gives me two errors.
    upload_2017-6-9_16-51-39.png
    both at that same line.

    Should I assign the audio to the audioSource in the inspector, and remove it from "Play();"?
     
  4. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    Yeah play works differently. You assign your audioclip to the audioSource, and then you call Play() (no parameters needed, but if you add one, it is a delay).
    Edit: I explained the parameter poorly.. i meant the parameter is optional, and the parameter is for delaying when the sound is played (but to complicate things, I guess the value you set here is based on the frequency of the sound clip - most people won't need to use this, I'd think).

    i.e., a basic set up would be:
    Code (csharp):
    1.  
    2. audioSource.clip = sound1; // you only need to do this once, unless your clip changes! so this would go in awake/start, or set in inspector
    3. audioSource.Play();
    4.  
    Just be aware that when you call Play on an AudioSource, it will interrupt sounds that are played through that same AudioSource (that also were called with Play... I don't want to get into it because you're already checking isPlaying anyways, which resolves that). Just ensure this specific AudioSource isn't used for other clips, and you're fine.
     
    Last edited: Jun 9, 2017
    Denisowator likes this.
  5. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    Thanks, it works now. :)
     
  6. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    Awesome!
     
  7. yashwanthd1998

    yashwanthd1998

    Joined:
    Sep 17, 2019
    Posts:
    7
    how.isnt it useful if u put the solution to others
     
  8. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,304
    Don't use PlayOneShot, because you don't have access to the audiosource used. It actually creates a new audiosource behind the scenes that you have no access to.

    Use Play instead.
     
  9. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    You can't sync it this way because frames have duration. when you start playing clip, it will end in M seconds. For instance, let M be 2 seconds. Your update function definitely will not be called right after two seconds. Update calls may happen at 1,998 and next will be 2.012 for instance, so 0.012 will be skipped. You can't go back in time for 12 milliseconds and start sound back then. If you want to loop the sound, set audio source loop property to true.