Ejemplo n.º 1
0
    private IEnumerator LoadAudio(string url, string clipName)
    {
        var       furl = "file:///" + url;
        AudioType type;

        switch (Path.GetExtension(url).ToLower())
        {
        case ".mp3":
            type = AudioType.MPEG;
            break;

        case ".wav":
            type = AudioType.WAV;
            break;

        case ".ogg":
            type = AudioType.OGGVORBIS;
            break;

        case ".xma":
            type = AudioType.XMA;
            break;

        default:
            type = AudioType.UNKNOWN;
            break;
        }

        using (var www = UnityWebRequestMultimedia.GetAudioClip(furl, type))
        {
            yield return(www.SendWebRequest());

            if (www.result == UnityWebRequest.Result.ConnectionError)
            {
                Debug.Log(www.error);
            }
            else
            {
                AudioClip ac;
                switch (type)
                {
                case AudioType.MPEG:     /*
                                          #if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
                                          * ac = NAudioPlayer.FromMp3Data(www.downloadHandler.data);
                                          * break;
                                          #else
                                          * Debug.LogError("MP3 Playback only supported on windows at the moment. Please contact me if you know how to convert MP3 to WAV bytes universally in Unity.");
                                          #endif*/
                    ac = NAudioPlayer.FromMp3Data(www.downloadHandler.data);
                    break;

                default:
                    ac = DownloadHandlerAudioClip.GetContent(www);
                    break;
                }

                ac.name = clipName;
                musicHandler.SongClip = ac;
                musicHandler.BeginGame();
            }
        }
    }