Пример #1
0
        private async UniTaskVoid ChooseSongsAndPlay()
        {
            // Fetch selected playlist tracks.
            FullPlaylist playlist = await SpotifyClient.Value.Playlists.Get(SelectedPlaylist.Value.Id);

            Paging <PlaylistTrack <IPlayableItem> >?tracksPaging = playlist.Tracks;

            if (tracksPaging == null)
            {
                throw new InvalidDataException();
            }

            List <PlaylistTrack <IPlayableItem> >?tracks = tracksPaging.Items;

            // Fetch two random songs.
            // TODO filter by sample
            FullTrack firstRandomSong  = (FullTrack)tracks.GetRandomElement().Track;
            FullTrack secondRandomSong = (FullTrack)tracks.GetRandomElement().Track;

            FirstSongSource.clip = await firstRandomSong.GetAudioClip();

            SecondSongSource.clip = await secondRandomSong.GetAudioClip();

            SelectedSongs.CorrectSongs = new List <string>
            {
                firstRandomSong.Name,
                secondRandomSong.Name
            };

            // Define correct songs (the ones that will play).
            SelectedSongs.Songs = new List <string>(SelectedSongs.CorrectSongs)
            {
                ((FullTrack)tracks.GetRandomElement().Track).Name,
                ((FullTrack)tracks.GetRandomElement().Track).Name
            };
            SelectedSongs.Songs.Shuffle();

            TrackAudioAnalysis firstSongAnalysis =
                await SpotifyClient.Value.Tracks.GetAudioAnalysis(firstRandomSong.Id);

            TrackAudioAnalysis secondSongAnalysis =
                await SpotifyClient.Value.Tracks.GetAudioAnalysis(firstRandomSong.Id);

            Segment firstSongRandomSegment  = firstSongAnalysis.Segments.GetRandomElement();
            Segment secondSongRandomSegment = secondSongAnalysis.Segments.GetRandomElement();

            FirstSongSource.pitch =
                secondSongRandomSegment.Pitches.Count > 0 ? secondSongRandomSegment.Pitches.Average() : 1f;
            SecondSongSource.pitch =
                firstSongRandomSegment.Pitches.Count > 0 ? firstSongRandomSegment.Pitches.Average() : 1f;

            FirstSongSource.pitch  = Mathf.Clamp(FirstSongSource.pitch, 0.5f, 1f);
            SecondSongSource.pitch = Mathf.Clamp(SecondSongSource.pitch, 0.5f, 1f);

            // Play the songs.
            FirstSongSource.Play();
            SecondSongSource.Play();

            OnSongsSelected.Raise();
        }
        private async Task UpdateTrackInfo(FullTrack track)
        {
            string trackId = track.Uri.Split(':').Last();

            if (trackId != _trackId)
            {
                UpdateBasicTrackInfo(track);

                try
                {
                    TrackAudioFeatures features = await _spotify.Tracks.GetAudioFeatures(trackId);

                    UpdateTrackFeatures(features);
                }
                catch (Exception e)
                {
                    _logger.Error("Error updating track audio features", e);
                }

                try
                {
                    _analysis = await _spotify.Tracks.GetAudioAnalysis(trackId);
                }
                catch (Exception e)
                {
                    _logger.Error("Error getting track audio analysis", e);
                }

                Image image = track.Album.Images.Last();
                if (image.Url != _albumArtUrl)
                {
                    await UpdateAlbumArtColors(image.Url);

                    _albumArtUrl = image.Url;
                }

                _trackId = trackId;
            }
        }