Beispiel #1
0
 public Tuple <Track, SessionContext> PullNextTrack()
 {
     if (CurrentManualQueue.HasItems)
     {
         return(new Tuple <Track, SessionContext>(CurrentManualQueue.Pull(), SessionContext.Queue));
     }
     else
     {
         return(new Tuple <Track, SessionContext>(CurrentBacklogQueue.Pull(), SessionContext.Backlog));
     }
 }
Beispiel #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);
                    }
                }
            });
        }