PlayOneShot getting cut off
I have a button that opens an inventory screen. I want to play a sound when the inventory opens. In the inventory script I have:
public AudioSource audio;
public AudioClip trunkOpen;
void OnEnable(){
audio.PlayOneShot(trunkOpen,1.0f);
}
Here is the clip I want to play:
Here is the audio source assigned to play it:
I've dragged the audio source, and the audio clip, to the inspector of the inventory script. The button enables the inventory screen. The sound is cutoff. I can't figure out why. There is no other sound playing.
Can you help?
Answer by Diukrone · Jan 17, 2018 at 06:11 PM
Maybe a audioS.priority = Xf; could help you! Put 10f on priority to get highest priority on play. Try change áudio file by WAV files. Low priority sounds sometimes are virtualized on Unity. MP3 files have some bug behaviours on Unity, try use WAV.
You can try also changing the khz of the áudio files attached on a GameObject, like this example: GetComponent().Play (56), 128, 256, etc... So use it to Split the bits that it was recorded to gain some delay.
Answer by Atliinux · Feb 16, 2020 at 10:07 PM
I've encountered a similar problem where the start of any audio I play with PlayOneShot() would occasionally get cut off. This was solved by playing the clips through AudioSource.Play() instead.
From my testing, it seems like this problem is caused by lag, since AudioSource.PlayOneShot() would cut itself short at the start of the clip if I had to do any heavy loading operation (i.e. loading an inventory) on the same frame that I call PlayOneShot(). AudioSource.Play() fixes this problem because it seems to depend on the frame completing before playing, meaning it will not start playing the clip until any loading operations on that frame are completed.