示例#1
0
        public async Task AddItemToQueue(SpotifyBaseObject item)
        {
            if (!allowedBacklogItems.Contains(item.Type.ToLower()))
            {
                throw new InvalidOperationException($"Cannot add a SpotifyBaseIrem of type \"{item.Type}\" to the manual queue");
            }

            if (item is Track singleTrack)
            {
                CurrentManualQueue.Push(singleTrack);
            }
            else
            {
                foreach (Track track in await ApiHelper.GetTracks(item, _user))
                {
                    CurrentManualQueue.Push(track);
                }
            }

            SessionStateChanged?.Invoke(this, EventArgs.Empty);
        }
示例#2
0
        private async void RunInternal()
        {
            await Task.Run(async() =>
            {
                _playbackState = await DataLoader.GetInstance().GetCurrentlyPlaying(true, (int)SESSION_NEXT_SONG_BEVOR_END_MS - 50);

                long lastUpadte = _unixTimestamp;

                string lastDeviceId = null;

                long tickStart = 0;

                SessionStateChanged?.Invoke(this, EventArgs.Empty);

                while (IsRunning)
                {
                    tickStart = Environment.TickCount;

                    if (AMOUNT_ITEMS_LEFT_TO_REPOPULATED_BACKLOG >= CurrentBacklogQueue.Count())
                    {
                        await RepopulateBacklog();
                    }

                    if (_unixTimestamp >= lastUpadte + SESSION_UPDATE_SLEEP_PAUSE_MS || //update when "updatetimer" tiggers
                        _possibleMSLeftInTrack < SESSION_NEXT_SONG_BEVOR_END_MS)    //update befor a PlayTrack to check whether playback is paused
                    {
                        await UpdateSession();
                        lastUpadte = _unixTimestamp;
                    }

                    //if (CurrentTrack == null || (_playbackState.Is_Playing && _possibleMSLeftInTrack < SESSION_NEXT_SONG_BEVOR_END_MS))
                    if (CurrentTrack == null || (_playbackState != null && CurrentTrack.Id == _playbackState.Item.Id && !_playbackState.Is_Playing && _playbackState.Progress_ms == 0))
                    {
                        Tuple <Track, SessionContext> nextTrackContext = PullNextTrack();

                        if (await PlayTrack(nextTrackContext))
                        {
                            Thread.Sleep(10);
                            await UpdateSession();
                        }
                        else
                        {
                            CurrentManualQueue.Push(nextTrackContext.Item1);
                        }
                    }

                    if (DeviceId != null && DeviceId != lastDeviceId)
                    {
                        await Controller.GetInstance().TransferPlayback(DeviceId, true);
                    }

                    lastDeviceId = DeviceId;

                    int sleepTime = (int)(SESSION_TICK_SLEEP_PAUSE_MS - (Environment.TickCount - tickStart));

                    if (sleepTime > 0)
                    {
                        Thread.Sleep(sleepTime);
                    }
                }
            });
        }