- Home /
AudioClip.Create() + PlayClipAtPoint() Not Working
Hi Unity friends,
I have a problem with loading and playing an audio file dynamically. I hope you can help! :-)
One. I have added an audio file called 01.wav to Resourses/SFX/ directory. The file is 44100HZ, stereo, 0:547 seconds long.
Two. The following MonoBehavior class works perfectly. I drag my audio asset on the "clip" field, then I call PlayAudioClip() and it plays it without a problem.
public class SoundController : MonoBehaviour
{
public AudioClip clip;
public void PlayAudioClip()
{
AudioSource.PlayClipAtPoint(clip, Camera.main.transform.position);
}
}
Three. But, when I try to dynamically create an AudioClip object from the file in a regular C# file (where there is opportunity to initialize an AudioClip with drag and drop), for some reason it doesn't play the sound for me. I can see the creation of "one shot audio" in the hierarchy, but no sound is played. Here is the code I'm trying:
AudioClip clip = AudioClip.Create("SFX/01", (int)(0.547 * 44100), 2, 44100, false, false);
AudioSource.PlayClipAtPoint(clip, Camera.main.transform.position);
I have tried various file formats and file settings, but they were not helpful. Also, I changed different address formats such as "Resources/SFX/01", "01" or "SFX/01.wav", but it didn't help either.
Thank you for your help! Cheers
Did you ever figure out any solution to this, @1h2o1o377? I'm currently having the same issue...
@jagels, I didn't find a way to solve the problem above, but I found another way to play clips: 1. Create a $$anonymous$$onoBehaviour class and attach its script to an object. 2. Add this public property to the class:
public List<AudioClip> SfxClips = new List<AudioClip>();
In the GUI drag and drop your audio files on top of the SfxClips list to populate it.
Add this method to your class to play sfx clips:
public void PlaySfxClip(int i) { AudioSource.PlayClipAtPoint(SfxClips[i], Camera.main.transform.position); }
Good luck! ;-) Cheers, Sia
public void PlaySfxClip(int i)
{
AudioSource.PlayClipAtPoint(SfxClips[i], Camera.main.transform.position);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613193413im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
audio.clip.GetData() returns no data. 0 Answers
Unity 5 low audio volume 1 Answer
Stopping audio source without stopping oneshot clips 0 Answers
Unity Audio Output Data 0 Answers
How can I check the scene and play audio accordingly? 1 Answer