IEnumerator Load()
        {
            var www = new WWW(urlToLoad);

            while (!www.isDone)
            {
                yield return(new WaitForEndOfFrame());
            }

            var config = new AudioLoaderConfig();

            config.DataStream = new MemoryStream(www.bytes);

            var loader = new AudioLoader(config);

            loader.StartLoading();
            while (!loader.IsLoadingDone)
            {
                yield return(new WaitForEndOfFrame());
            }

            var audioSource = GetComponent <AudioSource>();

            audioSource.clip = loader.AudioClip;
            audioSource.Play();
        }
        // Unity unifies file system access in the WWW class, we can use the WWW class to get data both from internet and from local file system on any platform.
        IEnumerator DownloadClipAndPlayFromUrl(string url)
        {
            if (lastAudioLoader != null)
            {
                lastAudioLoader.Destroy();
            }
            lastAudioLoader  = null;
            audioSource.clip = null;

            var isFromInternet = url.IndexOf("http", StringComparison.InvariantCultureIgnoreCase) != -1;

            statusMessage = "Getting data " + url;
            var www = new WWW(url);

            while (!www.isDone)
            {
                if (isFromInternet)
                {
                    statusMessage = "Downloading " + url + "\nDownload progress: " + (int)(www.progress * 100) + "%";
                }
                yield return(new WaitForEndOfFrame());
            }

            if (www.bytes.Length == 0)
            {
                statusMessage = "Error: downloaded zero bytes from " + url;
            }
            else
            {
                statusMessage = "Loading " + url;

                if (loadMethod.value == LoadMethod.StreamInUnityThread)
                {
                    statusMessage = "Streaming";
                }
                else
                {
                    loadTimer = Stopwatch.StartNew();
                }

                var config = new AudioLoaderConfig();
                config.DataStream         = new MemoryStream(www.bytes);
                config.PreferredDecoder   = preferredDecoder.value;
                config.UnityAudioClipName = url;
                config.LoadMethod         = loadMethod.value;

                lastAudioLoader = new AudioLoader(config);
                lastAudioLoader.OnLoadingAborted += () => statusMessage = "Loading aborted.";
                lastAudioLoader.OnLoadingDone    += () => statusMessage = "Loading done.";
                lastAudioLoader.OnLoadingFailed  += (exception) => statusMessage = "Loading has failed: " + exception.Message;
                audioSource.clip = lastAudioLoader.AudioClip;
                lastAudioLoader.StartLoading();
            }
        }
Beispiel #3
0
    public void Load()
    {
        songLibrary       = new SongLibrary();
        songLibrary.songs = new List <Song>();

        RyansMIDILibrary MIDI = new RyansMIDILibrary();

        if (baked) //Files are set in the inspector
        {
            foreach (SongConfig config in bakedTracks)
            {
                LAVA lavaParser = new LAVA();
                lavaParser.Load(config.songFile.bytes);
                //Debug.Log(lavaParser.oggOffset + " | " + lavaParser.oggLength + " | " + lavaParser.midiOffset + " | " + lavaParser.midiLength);

                //Parse MIDI
                Song midi = MIDI.LoadMIDI(lavaParser.midiData);
                for (int i = 0; i < 3; i++)
                {
                    config.song.difficulties[i] = midi.difficulties[i];
                }
                config.song.tempoEvents = midi.tempoEvents;

                //Parse OGG
                var audioConfig = new AudioLoaderConfig();
                audioConfig.DataStream = new MemoryStream(lavaParser.oggData);
                config.song.loader     = new AudioLoader(audioConfig);
                config.song.loader.StartLoading();

                songLibrary.songs.Add(config.song);
            }
        }
        else //Files are downloaded to the persistent data path
        {
        }
    }