Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Audio PlayOneShot() and Play() at the same time - not working. A bug?

Discussion in 'Audio & Video' started by larku, May 12, 2017.

  1. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    Is this a bug? (Unity 5.5.2p4)

    Trying to get an AudioSource to both PlayOneShot() and audioSource.Play() at the same time but it just refuses to work.

    Given code:
    Code (csharp):
    1. audioSource.PlayOneShot(myOneShotClip);
    2. audioSource.clip=clip; // some other sfx I want to start at the same time.
    3. audioSource.looping=true;
    4. audioSource.Play();
    The PlayOneShot(...) never plays.

    Removing the audioSource.clip=clip and the PlayOneShot(...) does play.

    Or if I reorder the calls so the PlayOneShot is last it also works fine:

    Code (csharp):
    1. audioSource.clip=clip; // some other sfx I want to start at the same time.
    2. audioSource.looping=true;
    3. audioSource.Play();
    4. audioSource.PlayOneShot(myOneShotClip);
    But it's very fragile having to ensure the order of events like this (this is a simplified example, and the situation is more convoluted than this - as such, re-ordering like this is not always guaranteed.

    Any ideas?
     
  2. grasan

    grasan

    Joined:
    Jun 6, 2013
    Posts:
    2
    I have the exact same problem whit my project.
    I even made an audio mixer, setted the output on the audio source to the new mixer. as i tried the game the mixer did not move an inch.

    I did noticed one thing, when the audio clip is supposed to play and i stop the test in the middle of the audio, i hear halv a second of audio, and the mixer moves.

    I have no clue why this doesn't work but i need this help to.
     
    Last edited: May 16, 2017
    larku likes this.
  3. aeclubwoi

    aeclubwoi

    Joined:
    May 12, 2017
    Posts:
    8
    Good reply !! Thank you.

    ดาวน์โหลด gclub
     
  4. lukitone

    lukitone

    Joined:
    Dec 25, 2011
    Posts:
    49
    are you using multiple audiosources or just one with multiple clips?
     
  5. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    I'm using multiple audio sources in this project, but for the context of this issue I'm it's all the same audio source (I have another AudioSource for music, but that's unrelated).
     
  6. lukitone

    lukitone

    Joined:
    Dec 25, 2011
    Posts:
    49
    Your AudioSource is probably busy playing the looping sound. An Audiosource can only play one sound at once. So use a second one for the other clip you want to play. PlayClipAtPoint(AudioClip clip, Vector3 position, float volume = 1.0F) would instantiate an audiosource for the clip you want to play and i believe it destroys it again when its done playing, thats also very handy for oneshots. hope this helped a bit
     
  7. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    Naa, one clip is PlayOneShot() the other is Play(), both work together and are supposed to.

    As per the original post, if I reverse the order of the calls it works fine. Also, if I put a frame or two delay between the calls it also works.
     
  8. lukitone

    lukitone

    Joined:
    Dec 25, 2011
    Posts:
    49
    yeah but the Audiosource is not the Audioclip

    1. audioSource.PlayOneShot(myOneShotClip);
    2. audioSource.clip=clip; // some other sfx want to start at the same time.//while the other clip is playing on the audiosource you change the clip of the audiosource right away
    3. audioSource.looping=true;
    4. audioSource.Play();
    Hmm thats all i can think of sorry :) i will also test this later
     
  9. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    No probs - I appreciate your input.

    I'm pretty sure AudioSources are supposed to be able to simultaneously play many PlayOneShot() clips at the same time as playing a single (Play()) clip. The only time this fails for me is if I attempt the calls in the order shown that fails and in the same frame. All other permutations seem to work without issue.

    Thanks again!
     
    lukitone likes this.
  10. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    The single Play() clips interrupt each other on an audiosource though, so what I assume is happening is it's also overriding any PlayOneShot() clips that the source uses. PlayOneShot() clips definitely play well with each other, but I'm not so sure Play() and PlayOneShot() are supposed to play well with each other.. but I don't know.

    You've got some pretty easy workarounds here though.... some ideas:

    1) Create one audio source for your one shots... another for your regular clips (if you don't mind them interrupting each other). This one strikes me as though it would do what you need, easily without really adding much in the way of overhead.
    2) Use PlayClipAtPoint, like @lukasgmusic suggested
    3) Create an array of audiosources, and have some sort of sound manager look after the one that gets used for playing a clip... i.e. loop through them, and check if it's playing sound currently.. if not playing.. use that source.
     
    lukitone likes this.
  11. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    Thanks cstooch,

    Indeed there are a number of workarounds (I've already worked around it). I'm really just wanting to know if it's a bug or just understand exactly why it doesn't work.

    Thanks for your help though!
     
    cstooch and lukitone like this.
  12. lukitone

    lukitone

    Joined:
    Dec 25, 2011
    Posts:
    49
    I don't think that's a bug. its just how it works. the first order you are doing, doesnt work because you change the audiosources clip right away, so the very first clip can't be played. the second order you don't do that. the audiosource keeps the assigned clip and plays a oneshot over it. you can whatch it in the inspector when an audiosource changes the clip.
     
    vampbatstudios, cstooch and larku like this.
  13. BaconNicholas

    BaconNicholas

    Joined:
    Nov 30, 2022
    Posts:
    2
    6 years later and I'm seeing this issue as well. I have an AudioSource attached to my player that plays while the user is holding down the space key. I am trying to use PlayClipAtPoint to instantiate another audiosource and play a oneshot when the player triggers a pickup object. The player's audio source stops playing and waits for the clip to play out, then begins playing again.
     
    Wilhelm_LAS likes this.