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
Answer by Statement · Oct 24, 2015 at 01:39 AM
Resources.Load will only work for assets stored in a Resources folder (see loading resources at runtime).
(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;
Thank you so much for taking your time ti answer me. I fallow this example from the link http://docs.unity3d.com/ScriptReference/WWW-audioClip.html
And unfortunately nothing happens. At least there is no error messages.
I don't know if i must use Application.dataPath , Application.persistentDataPath or simply a folder from my c: drive "c:/AudioFiles/audio1.wav"
If you can try to play an audio file in your computer from an empty Unity project and try the scenario that makes you help me better.
Here is my script:
using UnityEngine; using System.Collections;
public class StringAudioConverter : $$anonymous$$onoBehaviour { public string url = Application.dataPath + "/Resources/audio1.wav"; //Application.persistentDataPath; //"C:/simFIL/Audio/audio2015-10-23 14h59m04s.wav"; public AudioSource source; private void Start() { WWW www = new WWW(url); source = GetComponent(); source.clip = www.audioClip; } private void Update() { if (!source.isPlaying && source.clip.isReadyToPlay) source.Play(); } }
===
Thank and have a nice week end.
Thank you so much for taking your time ti answer me. I fallow this example from the link http://docs.unity3d.com/ScriptReference/WWW-audioClip.html
And unfortunately nothing happens. At least there is no error messages.
I don't know if i must use Application.dataPath , Application.persistentDataPath or simply a folder from my c: drive "c:/AudioFiles/audio1.wav"
If you can try to play an audio file in your computer from an empty Unity project and try the scenario that makes you help me better.
Thanks.
Phil
Prefix path with "file:///" as the reference says.
"file:///C:/AudioFiles/audio1.wav"
Does this still work? I'm able to load an AudioClip, but its empty, the name is "".
Got it working, had to add this:
WWW a = new WWW(url);
while (a.progress < 0.1)
{
yield return new WaitForSeconds(0.1f);
}
Answer by phildragon · Oct 25, 2015 at 09:11 PM
For people who got this issue or similar issue in the future, i want to let you know that the tutorial from the link http://docs.unity3d.com/ScriptReference/WWW-audioClip.html 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: http://answers.unity3d.com/questions/652919/music-player-get-songs-from-directory.html
that concerns a list of audioclip with a UI (OnGUI) in it but it is very helpfull. Thanks.
Phil
I appreciate your dedication on trying to warn people, but....
string link = "";
string path = "file:///c:/Audio/";
string filename = "test.wav";
link = path + filename;
...and ...
string link = "file:///c:/Audio/test.wav";
Produce the exact same string and doesn't affect the operation of WWW. Perhaps you had an error when you were testing it out or you were getting paths from another API which return backslashes "\" ins$$anonymous$$d of "/", or you made an error concatenating the strings.
I agree that it's the same thing ... and i believe that it's a bug from Unity. But i test it many times and i got that unpredictable issue. I didn't make any error regarding the "\" and "/". If it's not a bug from Unity, it's an issue with my machine because i didn't test it to another PC.
If that was a bug, it would mean that $$anonymous$$ono would be broken at its very core and nothing would work.
Thanks a lot Statement. The info that you provide was very helpful. Yeah sometimes machines are like humans: they work only when they want to.
Your answer
Follow this Question
Related Questions
Have object fly towards camera 0 Answers
Animation only plays in one of the objects 1 Answer
Load scene after battle 0 Answers
Action (by collider) 0 Answers