统一在运行时加载MP3文件 [英] Loading MP3 files at runtime in unity

查看:97
本文介绍了统一在运行时加载MP3文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 WWW 来在运行时加载mp3文件. 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");
        }
    }
}

推荐答案

如上所述,Windows不支持MP3,因此请使用OGG或WAV. 此方法实际上适用于mp3,我在Unity 2019.3.6f上对多个文件进行了测试

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

在访问剪辑之前,您必须等待WWW完成.而且必须将WWW加载到异步协程中.

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;
}

这篇关于统一在运行时加载MP3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆