- Home /
Issues importing AudioClip at runtime using WWW class
Hi,
I am having an issue with loading in an OGG audio file to my project. Basically I want to set my game up so that players can drop audio files into a folder called Playlist in the game directory then they can listen to this music while they play.
For some reason my audio clip never becomes instantiated. I have an Audio Source in my scene with this script attached to it.
As you can see I have commented out some code for the time being as I am trying to get a simple case of only 1 audio file playing at this stage.
So my question is what exactly am I doing wrong here? I have printed out the file path and it comes out as file://B:/Users/Matt/Development/Unity Projects/Space Game/Playlist/19_Tumbling_Dice.ogg which is the location to my audio.
However when I print out the audioLoader.audioClip.name it comes out as blank...
[RequireComponent(typeof(AudioSource))]
public class MP3AudioImporter : MonoBehaviour {
private List<string> playlist;
private string musicDir = "B:/Users/Matt/Development/Unity Projects/Space Game/Playlist/";
private int currIndex = 0;
void Start()
{
string[] songs = Directory.GetFiles(@musicDir, "*.ogg", SearchOption.TopDirectoryOnly);
playlist = new List<string>(songs);
StartAudio();
}
void StartAudio()
{
WWW audioLoader = new WWW("file://" + playlist[currIndex]);
Debug.Log(audioLoader.audioClip.name);
audio.clip = audioLoader.audioClip;
audio.Play();
/*
currIndex++;
if (currIndex > playlist.Count)
currIndex = 0;
}
Invoke("StartAudio", audio.clip.length + 0.5f);
*/
}
}
Answer by Graham-Dunnett · Jul 21, 2013 at 08:55 AM
You have to wait for the WWW
to complete before you try and access the data. The documentation has an example you can copy. Also, there is a WWW.GetAudioClip
function you can use to try and start accessing the audio before its fully loaded.
Your answer
![](https://koobas.hobune.stream/wayback/20220613110530im_/https://answers.unity.com/themes/thub/images/avi.jpg)