c# Script How to create an AudioClip from the file's path location ?

Let’s says i have a wav file in this current location “C:\AudioTest” and from Unity from c# script, i want to create an AudioClip instance from this audio file. is:

I tried this:
audioclip = (AudioClip)Resources.Load(path_file_name, typeof(AudioClip));

Nothing happens.

From Unity hierarchy i have an AudioSource component in my GameObject and no matter if i put an audio file or not in the AudioSource component, nothing happens.

Can someone help me ?

Thanks you for your attention.

Greetings.

Phil

For people who got this issue or similar issue in the future, i want to let you know that the tutorial from the link Unity - Scripting API: WWW.audioClip is a good reference but it has some limitation.

Indeed the contructor WWW( string url_path) can only take constant string value or a string variable which has only one value (like this “file:///c:/Audio/test.wav”). It will not work if you do something like this:
string link = “”;
string path = “file:///c:/Audio/”;
string filename = “test.wav”
link = path + filename;

WWW www = new WWW( link );

  • nothing will happens without any error message if you compile your program. It will work only if you use this:
    string link = “file:///c:/Audio/test.wav”;
    WWW www = new WWW( link );

or this
WWW www = new WWW( “file:///c:/Audio/test.wav” );

If you want to proceed with editable variable, use this tutorial from the link
references: Music player - (load sound files at runtime from directory, and play them) - Questions & Answers - Unity Discussions

that concerns a list of audioclip with a UI (OnGUI) in it but it is very helpfull.
Thanks.

Phil

Resources.Load will only work for assets stored in a Resources folder (see loading resources at runtime).

Use WWW.audioClip.

(Yes, it’s a funny name for a class that can load audio clips from disk)

Prefix path with “file:///” as the reference says.

string path = "C:/AudioFiles/audio1.wav";
string url = "file:///" + path;

WWW is now obsolete and UnityWebRequest has to be used instead.

if u need simpe way!