0
\$\begingroup\$

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);
        */
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ What unity are you using? \$\endgroup\$
    – Katu
    Commented Jul 21, 2013 at 6:51
  • \$\begingroup\$ @Katu Version 4.0 \$\endgroup\$ Commented Jul 22, 2013 at 6:12

1 Answer 1

2
\$\begingroup\$

WWW is processed on separate thread, asyncronously, so its not ready yet, when the next line executes. You have to yield after www, to make sure its done. WWW class does not contain "audioClip" member, but there is "GetAudioClip()" method.

void IEnumerator StartAudio()
{
    WWW audioLoader = new WWW("file://" + playlist[currIndex]);
    while( !audioLoader.isDone )
        yield return null;

    Debug.Log(audioLoader.GetAudioClip().name);
    audio.clip = audioLoader.GetAudioClip();
    audio.Play();

}

Then you can just call this as coroutine in Start()

void Start()
{
    StartCoroutine(StartAudio());
}

To play full playlist, you would have to download and save the files to audio list. Then you you could start playing em, with coroutine, just like how the StartAudio now. You would play one song from the list and "yield WaitForSeconds (audio.length )". So something like this.

Downloading to list:

IEnumerator DownloadPlaylist()
{
    string[] playlist = Directory.GetFiles(@musicDir, "*.ogg", SearchOption.TopDirectoryOnly);

    foreach(string song in playlist)
    {
        WWW audioLoader = new WWW("file://" + song);

        while( !audioLoader.isDone )
            yield return null;

        audioClips.Add(audioLoader.GetAudioClip(false));
    }   
}

Playing from list

IEnumerator PlayAudioList()
{   
    yield return StartCoroutine("DownloadPlaylist");

    foreach(AudioClip song in audioClips)
    {
        audio.clip = song;
        audio.Play();
        yield return new WaitForSeconds(song.length);
    }
}

void Start()
{
    StartCoroutine(PlayAudiolist());
}

Unity WWW class Documentation

\$\endgroup\$
3
  • \$\begingroup\$ Thanks, will check this out tonight. I assume that the while(!myW.isDone) is meant to read as while(!audioLoader.isDone). How will I need to adjust the logic if I want to play a playlist of audio? \$\endgroup\$ Commented Jul 22, 2013 at 1:39
  • 1
    \$\begingroup\$ Ah.. indeed ( fast copy/paste :P ) \$\endgroup\$
    – Katu
    Commented Jul 22, 2013 at 5:14
  • 1
    \$\begingroup\$ Added the playing of playlist. Just a note thou. I havent actually tested this code, but should work as is. \$\endgroup\$
    – Katu
    Commented Jul 22, 2013 at 5:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .