I’m using the WWW class to load a wav file directly, and get out an AudioClip from it. But for some reason I can’t get it to play.
IEnumerator Start() {
WWW wtf = new WWW("file:///" + Application.dataPath + "/SceneAssets/Audio/LDAudio_wtfDefault.wav");
yield return wtf;
wtfClip = wtf.audioClip;
Debug.Log(wtfClip);
}
public void playTest() {
Debug.Log("play test");
AudioSource.PlayClipAtPoint(wtfClip,new Vector3(0,0,0));
}
For some reason I can’t get the audio to play, even though I have a valid AudioSource instance.
Any ideas?
Thanks!
I got to tinkering with it… and I find that this:
WWW wtf = new WWW("file:///" + Application.dataPath + "/SceneAssets/Audio/LDAudio_wtfDefault.wav");
should be
WWW wtf = new WWW("file://" + Application.dataPath + "/SceneAssets/Audio/LDAudio_wtfDefault.wav");
bigmisterb:
I got to tinkering with it… and I find that this:
WWW wtf = new WWW("file:///" + Application.dataPath + "/SceneAssets/Audio/LDAudio_wtfDefault.wav");
should be
WWW wtf = new WWW("file://" + Application.dataPath + "/SceneAssets/Audio/LDAudio_wtfDefault.wav");
Thanks for catching that. No go though. I think I would have had a null reference to wtfClip if that was an issue as well.
Here is my test script that did work:
var numberOfSounds=22;
private var sounds : AudioClip[];
function Start () {
print(Application.dataPath);
sounds=new AudioClip[numberOfSounds];
for(var i=0; i<numberOfSounds; i++){
var s : String="00" + (i + 1);
s="file://" + Application.dataPath + "/Sounds/sound" + s.Substring(s.Length-2)+".wav";
var www = new WWW(s);
while(!www.isDone)
yield;
sounds[i] = www.GetAudioClip(false);
}
}
function Update () {
if(Input.GetMouseButtonDown(0)){
var go=new GameObject();
var a=go.AddComponent(AudioSource);
var clip : AudioClip=sounds[Random.value * numberOfSounds];
print(clip.length);
go.audio.clip=clip;
go.audio.Play();
Destroy(go, clip.length);
}
}
I have a folder filled with 22 sounds labeled sound01…sound22
Ahh… its the www.audioClip part. It assumes 3d sound is true, so if you put it on an object that is more than like 2 units away from the listener then you wont hear anything. I used www.GetAudioClip(false), forcing it to not be a 3d sound.