Sound player

StackOverflow thread

If you want to listen to dynamically loaded sounds in unity.

R = Refresh sounds from folder LEFT/RIGHT = clip selection UP/DOWN = source volume when playing Space = start playing selected clip

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;

/*
 * File management -> http://answers.unity3d.com/questions/16433/get-list-of-all-files-in-a-directory.html
 */

public class SoundPlayer : MonoBehaviour {

    string absolutePath = "./";

    public AudioSource src;
    List<AudioClip> clips = new List<AudioClip>();
    int soundIndex = 0;

    FileInfo[] files;

    void Start () {
        if(Application.isEditor)    absolutePath = "Assets/";
        //import snd
        reloadSounds();
    }

    void reloadSounds() {
        DirectoryInfo info = new DirectoryInfo(absolutePath);
        files = info.GetFiles();

        while(clips.Count > 0){
            AudioClip clip = clips[0];
            GameObject.DestroyImmediate(clip);
            clips.RemoveAt(0);
        }
        clips.Clear();

        foreach(FileInfo f in files){
            if(f.FullName.IndexOf(".ogg") > -1){
                //Debug.Log("Start loading "+f.FullName);
                StartCoroutine(loadFile(f.FullName));
            }
        }

        if(hasSound()){
            Debug.LogWarning("No sound loaded");
        }
    }

    void loadFileSystem(FileInfo f){
        string filePath = "";
        if(Application.isEditor){
            filePath = absolutePath+f.Name;
        }else{
            filePath = f.FullName;
            filePath = filePath.Replace('\\', '/');
        }

        Debug.Log("Loading "+filePath);
        Object obj = Resources.LoadAssetAtPath(filePath, typeof(AudioClip));
        if(obj != null) clips.Add((AudioClip)GameObject.Instantiate(obj));
        else Debug.LogWarning("obj is null");
    }

    IEnumerator loadFile(string path){
        Debug.Log("Start loading "+path);
        WWW www = new WWW("file://"+path);

        AudioClip myAudioClip = www.audioClip;
        while (!myAudioClip.isReadyToPlay)
        yield return www;

        AudioClip clip = www.GetAudioClip(false);
        string[] parts = path.Split('\\');
        clip.name = parts[parts.Length - 1];
        Debug.Log("Loaded "+clip.name);
        clips.Add(clip);
    }

    bool hasSound(){
        return clips.Count > 0;
    }

    void Update () {

        if(Input.GetKeyUp(KeyCode.R)){
            reloadSounds();
        }

        if(!hasSound()) return;

        if(Input.GetKeyUp(KeyCode.RightArrow)){
            nextSound();
        }else if(Input.GetKeyUp(KeyCode.LeftArrow)){
            prevSound();
        }

        if(Input.GetKeyUp(KeyCode.UpArrow)){
            changeVolume(0.1f);
        }else if(Input.GetKeyUp(KeyCode.DownArrow)){
            changeVolume(-0.1f);
        }

        if(Input.GetKeyUp(KeyCode.Space)){
            play();
        }
    }

    void changeVolume(float step){
        if(!src.isPlaying)  return;
        if(src.clip == null) return;

        src.volume += step;
        src.volume = Mathf.Clamp(src.volume, 0f,3f);
    }

    void play(){
        Debug.Log("Play "+soundIndex+", "+clips[soundIndex]);
        src.Stop();
        src.clip = clips[soundIndex];
        src.loop = true;
        src.Play();
    }

    void nextSound(){
        soundIndex++;
        if(soundIndex > clips.Count - 1)    soundIndex = 0;
    }
    void prevSound(){
        soundIndex--;
        if(soundIndex < 0)  soundIndex = clips.Count - 1;
    }

    void OnGUI(){
        string content = "v0.4";

        if(!hasSound()){
            content += " || no sound loaded";
            if(files != null){
                foreach(FileInfo f in files){
                    content += "\n"+f.FullName;
                }
            }
        }else{
            content += " || "+clips.Count+" sound(s) loaded";

            foreach(AudioClip clip in clips){
                content += "\nCLIP = "+clip.name;
            }

        }

        content += "\n\n[PLAYER]";

        if(hasSound()){
            if(src.isPlaying)   content += "\nplaying (volume:"+src.volume+") : "+src.clip.name;
            else content += "\nnot playing any sound";

            content += "\nselection ("+soundIndex+") name : "+clips[soundIndex].name;
        }

        GUI.Label(new Rect(0,0,800,800), content);
    }
}