Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading MP3 files at runtime in unity

I'm trying to load an mp3 file at runtime using the WWW class that is provided in Unity.

I don't get any errors, but I'm unable to play the music after the song has been processed. I have looked everywhere and cannot find anything to help me.

Here is the code I am currently using:

public class OpenFile : MonoBehaviour {
    string path;
    string extension;
    public GameObject musicAnalysis;
    string songName;
    float length;
    AudioSource song;

    // Use this for initialization
    void Start () {
        song =  musicAnalysis.GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update () {
        if(song.isPlaying != true){
            song.Play();
        }
    }

    public void FileSelect(){
        //Open windows Exploer 
        path = EditorUtility.OpenFilePanel("Select a Song","","");

        print(path);

        //Take the end of the the path and sasve it to another string
        extension = path.Substring(path.IndexOf('.') + 1);

        print (extension);
        //Check if the user has select the correct file
        if(extension == "mp3" || extension == "wav" || extension == "ogg"){
            //if correct file process file
            print ("You have selected the correct file type congrats");

            LoadSong();
            print ("Song Name: " + songName);
            print ("Song Length: " + length);
        }
        //if the user selects the wrong file type
        else{
            //pop up box that tells the user that they have selected the wrong file
            EditorUtility.DisplayDialog("Error","Incorrect File Type Please select another","Ok");
            ////Open windows Exploer 
            path = EditorUtility.OpenFilePanel("Select a Song","","");
        }
    }

    void LoadSong(){
        WWW www = new WWW("file://" + path);
        song.clip = www.audioClip;
        songName =  www.audioClip.name;
        length = www.audioClip.length;

        while(!www.isDone){
            print ("Processing File" + path);
        }

        if(www.isDone == true){
            print ("Song has been processed");
        }
    }
}
like image 444
user2552211 Avatar asked Oct 29 '25 09:10

user2552211


1 Answers

As noted above, windows doesn't support MP3, so use OGG or WAV. Edit: This method do in fact work with mp3, I tested it with multiple files on Unity 2019.3.6f

You must wait for WWW to complete before accessing the clip. And WWW must be loaded in an asynchronouse Coroutine.

public void LoadSong()
{
    StartCoroutine(LoadSongCoroutine());    
}

IEnumerator LoadSongCoroutine()
{
    string url = string.Format("file://{0}", path); 
    WWW www = new WWW(url);
    yield return www;

    song.clip = www.GetAudioClip(false, false);
    songName =  song.clip.name;
    length = song.clip.length;
}
like image 79
peterept Avatar answered Oct 31 '25 13:10

peterept



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!