コード例 #1
0
ファイル: SpotifyService.cs プロジェクト: Ringobot/ringo
        public async Task TurnOffShuffleRepeat(string token, CurrentPlaybackContext info)
        {
            // turn off shuffle and repeat
            if (info.ShuffleState)
            {
                await _player.Shuffle(false, accessToken : token);

                //await _player.Shuffle(false, accessToken: token, deviceId: info.Device.Id);
            }

            if (info.RepeatState != RepeatStates.Off)
            {
                await _player.Repeat(RepeatStates.Off, accessToken : token);

                //await _player.Repeat(RepeatStates.Off, accessToken: token, deviceId: info.Device.Id);
            }
        }
コード例 #2
0
        public async Task ReorderPlaylist(IPlaylist playlist, Room room, RoomSong song)
        {
            CurrentPlaybackContext currentPlayback = null;

            try
            {
                var            http   = new HttpClient();
                IConfiguration config = new ConfigurationBuilder()
                                        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                                        .AddEnvironmentVariables()
                                        .Build();

                var player = new PlayerApi(http, AuthToken);
                currentPlayback = await player.GetCurrentPlaybackInfo(AuthToken);
            }
            catch (Exception ex)
            {
            }

            try
            {
                var client = new RestClient(@"https://api.spotify.com/v1");
                client.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator($"Bearer {AuthToken}");
                var request = new RestRequest($@"playlists/{playlist.PlaylistID}/tracks", Method.PUT);
                request.RequestFormat = DataFormat.Json;
                if (song != null)
                {
                    room.RoomSongs.SingleOrDefault(s => s.SongId == song.SongId).SongRating -= 1;
                }
                var spotifyUris = room.RoomSongs
                                  .OrderByDescending(s => s.SongRating)
                                  .Select(s => s.Song.SpotifyId)
                                  .ToList();
                if (currentPlayback != null && spotifyUris.Any(s => s == currentPlayback.Item.Uri))
                {
                    // Reorder songs, only after the current one we are playing.
                    // This ensures that if you're in the middle of a playlist,
                    // the next song you listen to is the highest rated song
                    for (int i = 0; i < spotifyUris.Count; ++i)
                    {
                        if (spotifyUris[0] == currentPlayback.Item.Uri)
                        {
                            break;
                        }
                        spotifyUris.Remove(spotifyUris[0]);
                    }
                }
                else
                {
                    spotifyUris = room.RoomSongs
                                  .OrderByDescending(s => s.SongRating)
                                  .Select(s => s.Song.SpotifyId)
                                  .ToList();
                }

                request.AddJsonBody(new { uris = spotifyUris });

                var response = await client.ExecuteTaskAsync(request);

                var content = response.Content;
            }
            catch (Exception ex)
            {
            }
        }