Search Unity

Sounds getting cuttoff with AudioSource.Play()

Discussion in 'Audio & Video' started by ninjapretzel, Apr 22, 2015.

  1. ninjapretzel

    ninjapretzel

    Joined:
    Jul 19, 2011
    Posts:
    27
    The gun sounds in this video are being played by AudioSource.Play()
    They are one-shot sound objects created in code when the gun fires.

    I instantiate a game object with the AudioSource attached and set up, and then assign the sound (loaded dynamically via resources, in the video, these are all already loaded) to the AudioSource's clip field, and then call the Play() function of the AudioSource.

    Sometimes, the begining of the sound is cut off when it is played. It's often enough that it doesn't sound right.
     
    JoRouss likes this.
  2. aihodge

    aihodge

    Joined:
    Nov 23, 2014
    Posts:
    163
    From where in your code are you calling Play() on the AudioSource?
     
  3. ninjapretzel

    ninjapretzel

    Joined:
    Jul 19, 2011
    Posts:
    27
    When the audio source is created.
    basically it's something like:


    AudioSource source = Instantiate(settings, pos, Quaternion.identity) as AudioSource;
    source.clip = sc;
    source.volume = Settings.soundVolume;
    source.Play();
     
  4. Lukas-Wendt

    Lukas-Wendt

    Joined:
    Jun 1, 2014
    Posts:
    13
    unitybru likes this.
  5. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
  6. chadfranklin47

    chadfranklin47

    Joined:
    Aug 11, 2015
    Posts:
    229
    Still getting this problem in Unity 2019.4 LTS. I'm starting the sound in a MEC Coroutine. Not sure if that is the reason.

    Edit: Changing the priority to 0 seems to have fixed the issue.
     
    Last edited: Jun 11, 2020
    harlandpj likes this.
  7. harlandpj

    harlandpj

    Joined:
    Jul 15, 2020
    Posts:
    6
    Interesting, will try that myself, I have tried every variation of Play(), PlayOneShot() with a Coroutine to delay anything else happening soundwise, used WaitForSeconds(clip.length), even added a little onto the delay time to see if it stops, and guess what... no luck so far. Will try changing priority and hope that works (first i've heard of it lol)!
     
  8. JohannaJacob

    JohannaJacob

    Joined:
    Jul 23, 2013
    Posts:
    3
    I also have the problem in Unity 2019.4 LTS. For some strange reason, AudioSource.PlayClipAtPoint works.
    I don't think starting the sound from a coroutine is the issue, because starting it from anywhere in the script causes issues, as well as the standard "play on awake" checkbox in the Audioclip itself.

    Changing the priority to 0 did not fix the issue in my case. Really annoying, but at least AudioSource.PlayClipAtPoint works.
     
  9. chadfranklin47

    chadfranklin47

    Joined:
    Aug 11, 2015
    Posts:
    229
    Switched my audio solution to FMOD a while back and could never go back to using Unity's default audio solution. If you have a day to spare to learn how to use it, I highly recommend making the switch. It's free (for small devs) if you haven't heard of it before. Haven't had one problem with audio since.
     
  10. PurpleGoop

    PurpleGoop

    Joined:
    Apr 30, 2018
    Posts:
    16
    Necroposting, but this issue is still relevant.
    I have the same problem, an array of 3 audiosources (to avoid cutting off the previous clip with .Play()) that i cycle through every shot. Randomly, it will cut off the start of the clip, the amount that is cut and the frequency is apparently completely random. I tried Stop() and also setting the time of the clip to 0 before playing it and it still cuts from time to time.I also found out about PlayOneShot thanks to Lukas and replaced it to the array system, but same problem.
    Its such a stupid yet devastating problem it drives me nuts.
    Did anyone actually find a proper solution to this?
    Can a man simply play an audio clip without problems o_O?
     
    Last edited: Feb 10, 2024
  11. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    421
    Hi @PurpleGoop

    Please submit a bug report if you're able to isolate the problem.

    Thanks!
     
    PurpleGoop likes this.
  12. I5

    I5

    Joined:
    Feb 15, 2015
    Posts:
    21
    SeventhString likes this.
  13. I5

    I5

    Joined:
    Feb 15, 2015
    Posts:
    21
    Here's an example of footstep sounds (using WAV files) sometimes being cutoff - code playing footstep sound is from Unity's HeadBob script (tried to upload vid with sound but wasn't able to) - when the character moves faster[r] almost all clips are cutoff at the end, but if the char moves very slowly (one step per 2 seconds), the clips don't always cuttoff (not sure if it's limited to only some clips though):

    Code (CSharp):
    1. public Vector3 bobHead(float speed) {
    2.             float xPos = m_OriginalCameraPosition.x + (animCurve.Evaluate(m_CyclePositionX) * horzBobRange);
    3.             float yPos = m_OriginalCameraPosition.y + (animCurve.Evaluate(m_CyclePositionY) * vertBobRange);
    4.  
    5.             m_CyclePositionX += (speed * Time.deltaTime) / m_BobBaseInterval;
    6.             m_CyclePositionY += ((speed * Time.deltaTime) / m_BobBaseInterval) * vertHorzRatio;
    7.  
    8.             if (m_CyclePositionX > m_Time) {
    9.                 m_CyclePositionX = m_CyclePositionX - m_Time;
    10.             }
    11.             if (m_CyclePositionY > m_Time) {
    12.                 m_CyclePositionY = m_CyclePositionY - m_Time;
    13.                 if (footsteps.Length > 1) footstepsAud.PlayOneShot(footsteps[UnityEngine.Random.Range(0, footsteps.Length)]);
    14.                 else if (footsteps.Length == 1) footstepsAud.PlayOneShot(footsteps[0]);
    15.             }
    16.  
    17.             return new Vector3(xPos, yPos, 0f);
    18.         }