【问题标题】:Loading MP3 files at runtime in unity在运行时统一加载 MP3 文件
【发布时间】:2020-11-16 06:11:36
【问题描述】:

我正在尝试使用 Unity 中提供的 WWW 类在运行时加载 mp3 文件。

我没有收到任何错误,但歌曲处理完毕后我无法播放音乐。我到处找,找不到任何可以帮助我的东西。

这是我目前使用的代码:

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。
  • 我使用的是 Windows。你知道我也尝试过 ogg 和 wav @peterept
  • 您绝对可以使用 ogg 或 wav(确保文件具有正确的扩展名)。您的代码在上面不起作用的原因是因为您没有等待 www 完成。下面我给你答案。

标签: c# unity3d


【解决方案1】:

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

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

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

【讨论】:

  • 非常感谢您的帮助。我现在可以处理 wav 文件。我还有一个问题,也许你可以帮助我。行 songName = song.clip.name;长度=歌曲.剪辑.长度;当我将它们打印到控制台时,什么也没有出现。@peterept
  • 长度应该是秒,但根据音频编码的编码,有时会返回null。
  • 在 Unity 2019 中不起作用,显示“不支持在此平台上流式传输 'mpeg'”
【解决方案2】:

不直接支持在 Unity 中的 Windows 中加载 mp3 的 AFAIK (2019.4.1)

使用 NLayer 库这对我有用

IEnumerator LoadHelper(string uri)
{
    UnityWebRequest www = UnityWebRequest.Get(uri);
    yield return www.SendWebRequest();

    if(www.isNetworkError || www.isHttpError)
    {
        Debug.Log(www.error);
    }
    else
    {
      // Or retrieve results as binary data
      byte[] results = www.downloadHandler.data;
      var memStream = new System.IO.MemoryStream(results);
      var mpgFile = new NLayer.MpegFile(memStream);
      var samples = new float[mpgFile.Length];
      mpgFile.ReadSamples(samples, 0, (int)mpgFile.Length);

      var clip = AudioClip.Create("foo", samples.Length, mpgFile.Channels, mpgFile.SampleRate, false);
      clip.SetData(samples, 0);
      source.clip = clip;
    }
}

source 作为AudioSource

示例项目 here 和 HTML 构建 here

【讨论】:

    【解决方案3】:

    您可以使用 AudioImporter (https://github.com/Hello-Meow/AudioImporter),并将支持与商业 BASS 库结合使用的 mp3(请参阅自述文件)。

    它说您可以在返回第一个缓冲区后立即开始播放生成的音频剪辑 - 我现在只是在自己测试。

    【讨论】:

      猜你喜欢
      • 2020-08-16
      • 1970-01-01
      • 2016-09-27
      • 1970-01-01
      • 2017-08-24
      • 1970-01-01
      • 2019-06-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多