Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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);
        */
    }
}
share|improve this question
    
What unity are you using? –  Katu Jul 21 '13 at 6:51
    
@Katu Version 4.0 –  Matthew Pigram Jul 22 '13 at 6:12

1 Answer 1

up vote 2 down vote accepted

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

share|improve this answer
    
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? –  Matthew Pigram Jul 22 '13 at 1:39
1  
Ah.. indeed ( fast copy/paste :P ) –  Katu Jul 22 '13 at 5:14
1  
Added the playing of playlist. Just a note thou. I havent actually tested this code, but should work as is. –  Katu Jul 22 '13 at 5:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.