Beispiel #1
0
        public async Task <SongRequestCurrentlyPlayingModel> GetStatus()
        {
            this.status = null;
            if (this.httpListenerServer != null)
            {
                await this.DispatchWrapper(() => this.browser.InvokeScript("getStatus"));

                for (int i = 0; i < 10 && this.status == null; i++)
                {
                    await Task.Delay(500);
                }
            }
            return(status);
        }
        private async Task BackgroundSongMaintainer(CancellationTokenSource cancellationTokenSource)
        {
            int failedStatusAttempts = 0;
            await BackgroundTaskWrapper.RunBackgroundTask(cancellationTokenSource, async (tokenSource) =>
            {
                await Task.Delay(backgroundInterval, tokenSource.Token);

                await SongRequestService.songRequestLock.WaitAndRelease(async() =>
                {
                    if (this.Status != null && this.Status.State == SongRequestStateEnum.Playing)
                    {
                        this.Status.Progress += backgroundInterval;
                    }

                    if (this.forceStateQuery || this.Status == null || (this.Status.Length > 0 && this.Status.Progress >= this.Status.Length))
                    {
                        this.forceStateQuery = false;
                        SongRequestCurrentlyPlayingModel newStatus = await this.GetStatus();
                        if (newStatus == null || newStatus.State == SongRequestStateEnum.NotStarted)
                        {
                            this.forceStateQuery = true;
                            failedStatusAttempts++;
                            if (failedStatusAttempts >= 3)
                            {
                                await this.SkipInternal();
                            }
                        }
                        else
                        {
                            failedStatusAttempts = 0;

                            this.Status.State    = newStatus.State;
                            this.Status.Progress = newStatus.Progress;
                            this.Status.Length   = newStatus.Length;
                            this.Status.Volume   = newStatus.Volume;

                            if (this.Status.State == SongRequestStateEnum.Ended)
                            {
                                await this.SkipInternal();
                            }
                            else if (this.Status.Volume != ChannelSession.Settings.SongRequestVolume)
                            {
                                await this.RefreshVolumeInternal();
                            }
                        }
                    }
                });
            });
        }
        private async Task BackgroundSongMaintainer(CancellationTokenSource cancellationTokenSource)
        {
            int totalLoops = 0;
            await BackgroundTaskWrapper.RunBackgroundTask(cancellationTokenSource, async (tokenSource) =>
            {
                await Task.Delay(backgroundInterval, tokenSource.Token);

                await SongRequestService.songRequestLock.WaitAndRelease(async() =>
                {
                    if (this.Status != null && this.Status.Length > 0)
                    {
                        totalLoops++;
                        if (this.Status.State == SongRequestStateEnum.Playing)
                        {
                            this.Status.Progress += backgroundInterval;
                            if (totalLoops >= 30)
                            {
                                SongRequestCurrentlyPlayingModel newStatus = await this.GetStatus();
                                if (newStatus != null)
                                {
                                    this.Status.State    = newStatus.State;
                                    this.Status.Progress = newStatus.Progress;
                                    this.Status.Length   = newStatus.Length;
                                    this.Status.Volume   = newStatus.Volume;
                                }
                                totalLoops = 0;
                            }
                        }

                        if (this.Status.Progress >= this.Status.Length || this.Status.State == SongRequestStateEnum.Ended)
                        {
                            await this.SkipInternal();
                        }
                    }
                    else
                    {
                        SongRequestCurrentlyPlayingModel newStatus = await this.GetStatus();
                        if (newStatus != null)
                        {
                            this.Status.State    = newStatus.State;
                            this.Status.Progress = newStatus.Progress;
                            this.Status.Length   = newStatus.Length;
                            this.Status.Volume   = newStatus.Volume;
                        }
                    }
                });
            });
        }
Beispiel #4
0
        public async Task <SongRequestCurrentlyPlayingModel> GetStatus()
        {
            try
            {
                SpotifyCurrentlyPlayingModel currentlyPlaying = await ChannelSession.Services.Spotify.GetCurrentlyPlaying();

                if (currentlyPlaying != null && currentlyPlaying.ID != null)
                {
                    SongRequestCurrentlyPlayingModel result = new SongRequestCurrentlyPlayingModel()
                    {
                        ID         = currentlyPlaying.ID,
                        URI        = currentlyPlaying.Uri,
                        Name       = currentlyPlaying.ToString(),
                        AlbumImage = (!string.IsNullOrEmpty(currentlyPlaying.Album?.ImageLink)) ? currentlyPlaying.Album?.ImageLink : SpotifyDefaultAlbumArt,
                        Type       = SongRequestServiceTypeEnum.Spotify,
                        Progress   = currentlyPlaying.CurrentProgress,
                        Length     = currentlyPlaying.Duration,
                        Volume     = currentlyPlaying.Volume,
                    };

                    if (currentlyPlaying.IsPlaying)
                    {
                        result.State = SongRequestStateEnum.Playing;
                    }
                    else if (currentlyPlaying.CurrentProgress > 0)
                    {
                        result.State = SongRequestStateEnum.Paused;
                    }
                    else
                    {
                        result.State = SongRequestStateEnum.Ended;
                    }

                    return(result);
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
            }
            return(null);
        }
Beispiel #5
0
 public void SetStatus(string result)
 {
     this.status = SerializerHelper.DeserializeFromString <SongRequestCurrentlyPlayingModel>(result);
 }