コード例 #1
0
    IEnumerator LoadMp3(FileInfo fileInfo, MP3ResBean res, Action <AudioResBean> callBack)
    {
        while (!res.isOk)
        {
            yield return(new WaitForSeconds(0.1f));
        }
        using (UnityWebRequest request = UnityWebRequestMultimedia.GetAudioClip("file://" + res.SavePath, AudioType.WAV))
        {
            yield return(request.SendWebRequest());

            if (request.isHttpError || request.isNetworkError)
            {
                MyLog.Red(request.error);
                yield break;
            }
            AudioClip clip = DownloadHandlerAudioClip.GetContent(request);
            clip.name = Path.GetFileNameWithoutExtension(res.SavePath);

            AudioResBean resBean = new AudioResBean(clip, res.YuanPath, res.SavePath, true, fileInfo);
            l_AudioResBean.Add(resBean);
            if (null != callBack)
            {
                callBack(resBean);
            }
        }
    }
コード例 #2
0
    public void StartLoadAudioClip(FileInfo fileInfo, Action <AudioResBean> callBack)
    {
        foreach (AudioResBean bean in l_AudioResBean)
        {
            if (fileInfo.FullName == bean.YuanPath)
            {
                callBack(bean);
                return;
            }
        }


        if (fileInfo.Extension == ".mp3")
        {
            MP3ResBean resBean = null;
            for (int i = 0; i < l_HasLoadMp3.Count; i++)
            {
                if (l_HasLoadMp3[i].YuanPath == fileInfo.FullName)
                {
                    resBean = l_HasLoadMp3[i];
                    break;
                }
            }
            if (null == resBean)
            {
                resBean = new MP3ResBean();
                string savePath = dirPath + "/" + Path.GetFileNameWithoutExtension(fileInfo.FullName) + ".wav";
                new Thread(() =>
                {
                    try
                    {
                        FileStream stream    = File.Open(fileInfo.FullName, FileMode.Open);
                        Mp3FileReader reader = new Mp3FileReader(stream);
                        WaveFileWriter.CreateWaveFile(savePath, reader);
                        resBean.YuanPath = fileInfo.FullName;
                        resBean.isOk     = true;
                        resBean.SavePath = savePath;
                        l_HasLoadMp3.Add(resBean);
                    }
                    catch (Exception e)
                    {
                        MyLog.Red("有错 —— " + e);
                        throw;
                    }
                }).Start();
            }
            Ctrl_Coroutine.Instance.StartCoroutine(LoadMp3(fileInfo, resBean, callBack));
        }
        else
        {
            Ctrl_Coroutine.Instance.StartCoroutine(LoadOtherGeShi(fileInfo, callBack));
        }
    }