Exemplo n.º 1
0
    /// <summary>
    /// 下载歌曲
    /// </summary>
    /// <param name="callback"></param>
    public JLoaderInfo[] DownloadSong(SongData data, bool isGetInfo, Action <float, SongInfo> callback)
    {
        //如果文件都存在本地
        SongInfo info = new SongInfo();

        info.data = data;

        bool   readLocal          = false;
        string persistentDataPath = AppContentPath();

        if (!data.local)
        {
            if ((File.Exists(persistentDataPath + data.enName + ".mp3") || File.Exists(persistentDataPath + data.enName + ".wav")) &&
                File.Exists(persistentDataPath + data.enName + "_easy.txt") &&
                File.Exists(persistentDataPath + data.enName + "_mid.txt") &&
                File.Exists(persistentDataPath + data.enName + "_hard.txt") &&
                File.Exists(persistentDataPath + data.cover + ".png"))
            {
                readLocal = true;
            }
        }

        //string[] downloadFileName = {  data.enName + ".mp3",
        //                         data.enName + "_easy.txt",
        //                         data.enName + "_mid.txt",
        //                        data.enName + "_hard.txt",
        //                        data.cover + ".png"
        //                            };
        string[] downloadFileName = { data.enName + ".wav",
                                      data.enName + "_easy.txt",
                                      data.enName + "_mid.txt",
                                      data.enName + "_hard.txt",
                                      data.cover + ".png" };

        float[] percents = new float[downloadFileName.Length];
        bool    failed   = false;

        JLoaderInfo[] infos = new JLoaderInfo[downloadFileName.Length];
        for (int i = 0; i < downloadFileName.Length; i++)
        {
            string n = downloadFileName[i];
            string p;
            if (readLocal)
            {
                p = AppContentPathOfWWW() + n;
            }
            else
            {
                if (!data.local)
                {
                    p = RES_HOST + "music/" + n;
                }
                else
                {
                    p = AppStreamPath("music/" + n);
                }
            }
            float[] pp = percents;
            Debug.Log("加载" + p);
            infos[i] = JLoader.instance.Load(p, i, (type, jinfo) =>
            {
                if (failed)
                {
                    return;
                }
                switch (type)
                {
                case JLoader.DOWNLOAD_TYPE.SUCCESS:
                    percents[(int)jinfo.userData] = 1;
                    if (isGetInfo)
                    {
                        if (p.IndexOf(".wav") != -1)
                        {
                            //info.songClip = jinfo.www.GetAudioClip(false);
                            info.songClip = DownloadHandlerAudioClip.GetContent(jinfo.www);
                        }
                        else if (p.IndexOf("_easy") != -1)
                        {
                            info.ParseEasy(jinfo.www.downloadHandler.text);
                        }
                        else if (p.IndexOf("_mid") != -1)
                        {
                            info.ParseMid(jinfo.www.downloadHandler.text);
                        }
                        else if (p.IndexOf("_hard") != -1)
                        {
                            info.ParseHard(jinfo.www.downloadHandler.text);
                        }
                        else if (p.IndexOf(".png") != -1)
                        {
                            info.bytes = jinfo.www.downloadHandler.data;
                        }
                    }

                    if (!readLocal && data.local == false)
                    {
                        string outfile = AppContentPath() + n;
                        string dir     = Path.GetDirectoryName(outfile);
                        if (!Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }
                        File.WriteAllBytes(outfile, jinfo.www.downloadHandler.data);
                    }
                    break;

                case JLoader.DOWNLOAD_TYPE.FAILED:
                    failed = true;
                    Debug.Log(p + " 加载失败~" + jinfo.www.error);
                    callback(-1, null);
                    break;
                }
                pp[(int)jinfo.userData] = jinfo.www.downloadProgress;
                bool allDone            = true;
                float allPer            = 0;
                for (int j = 0; j < percents.Length; j++)
                {
                    if (allDone && percents[j] != 1)
                    {
                        allDone = false;
                    }
                    if (percents[j] != -1)
                    {
                        allPer += percents[j];
                    }
                }
                if (allPer == -1 * downloadFileName.Length)
                {
                    callback(-1, null);
                }
                else
                {
                    callback(allPer / (float)percents.Length, info);
                }
            });
        }
        return(infos);
    }