Пример #1
0
        public async Task ChangePlaybackTrack(CancellationToken token)
        {
            string         song  = State.QueueData[State.CurrentQueueIndex].Id;
            RefCountedClip rClip = await GetSongClip(song, token);

            rClip.UpdateLastUsedTime();

            State.CurrentClip = rClip;
            State.TrackTime   = 0;

            AudioPlayer.Instance.SetMusic(State.CurrentClip.Clip, MusicSlot.User, State.Volume, false, true);
            AudioPlayer.Instance.StartMusic(MusicSlot.User, true);
            AudioPlayer.Instance.SeekMusic(MusicSlot.User, State.TrackTime);

            TrimClipCache(); //?
        }
Пример #2
0
        /// <summary>
        /// Gets an AudioClip for a song
        /// </summary>
        /// <remarks>
        /// Run this from the main thread and only from the main thread!
        /// </remarks>
        public async Task <RefCountedClip> GetSongClip(string song, CancellationToken token)
        {
            if (!AsyncUtils.IsOnMainThread())
            {
                throw new InvalidOperationException("GetSongClip must be run from the main thread. I don't make the rules, sorry.");
            }

            if (State.ClipCache.ContainsKey(song))
            {
                return(State.ClipCache[song]);
            }

            //request the server transcode
            var request = new RestRequest($"library/song/{song}", HttpMethod.Get, null, new KeyValuePair <string, string>("transcode", "wave"),
                                          new KeyValuePair <string, string>("return", "path"));

            RestResponse response = default;
            await Task.Run(async() => response = await DoRestRequest(request, 100000, token));

            var jroot = JToken.Parse(response.Body);

            if (jroot.IsNullOrEmpty() || jroot["data"].IsNullOrEmpty() || jroot["data"]["transcodedPath"].IsNullOrEmpty())
            {
                return(null);
            }

            string path = jroot["data"]["transcodedPath"].ToString();

            //load the clip
            var clip  = WaveLoader.WaveLoader.LoadWaveToAudioClip(path, song); //will probably stutter, we'll optimize it later
            var rClip = new RefCountedClip(clip);

            State.ClipCache.Add(song, rClip);

            return(rClip);
        }