đź–¤Getakoroneđź–¤
đź–¤Getakoroneđź–¤
CC#
Created by đź–¤Getakoroneđź–¤ on 6/24/2023 in #help
âť” I want to only have hours, minutes, secs, on my count up for my music:
here code:
private async Task UpdateSongPositionAndDuration(string accessToken)
{
string playbackEndpoint = "https://api.spotify.com/v1/me/player/currently-playing";
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var response = await _httpClient.GetAsync(playbackEndpoint);
var responseString = await response.Content.ReadAsStringAsync();
var responseObject = JObject.Parse(responseString);
if (responseObject != null && responseObject["progress_ms"] != null)
{
// Assuming you have retrieved the current position from the Spotify API response
TimeSpan songPosition = TimeSpan.FromMilliseconds((double)responseObject["progress_ms"]);
TimeSpan songDuration = TimeSpan.FromMilliseconds((double)responseObject["item"]["duration_ms"]);


// Set the song duration and position
SongDuration = songDuration;
SongPosition = songPosition;

// Calculate the progress percentage
// Calculate the progress percentage
TimeSpan time = DateTime.Now.TimeOfDay;
TimeSpan truncatedTime = TimeSpan.FromSeconds(Math.Truncate(time.TotalSeconds));
TimeSpan songProgress = new TimeSpan(truncatedTime.Hours, truncatedTime.Minutes, truncatedTime.Seconds);
SongProgress = songProgress;



}
else
{
// Set default values when data is not available
SongDuration = TimeSpan.Zero;
SongPosition = TimeSpan.Zero;
SongProgress = TimeSpan.Zero;
}
}
private async Task UpdateSongPositionAndDuration(string accessToken)
{
string playbackEndpoint = "https://api.spotify.com/v1/me/player/currently-playing";
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var response = await _httpClient.GetAsync(playbackEndpoint);
var responseString = await response.Content.ReadAsStringAsync();
var responseObject = JObject.Parse(responseString);
if (responseObject != null && responseObject["progress_ms"] != null)
{
// Assuming you have retrieved the current position from the Spotify API response
TimeSpan songPosition = TimeSpan.FromMilliseconds((double)responseObject["progress_ms"]);
TimeSpan songDuration = TimeSpan.FromMilliseconds((double)responseObject["item"]["duration_ms"]);


// Set the song duration and position
SongDuration = songDuration;
SongPosition = songPosition;

// Calculate the progress percentage
// Calculate the progress percentage
TimeSpan time = DateTime.Now.TimeOfDay;
TimeSpan truncatedTime = TimeSpan.FromSeconds(Math.Truncate(time.TotalSeconds));
TimeSpan songProgress = new TimeSpan(truncatedTime.Hours, truncatedTime.Minutes, truncatedTime.Seconds);
SongProgress = songProgress;



}
else
{
// Set default values when data is not available
SongDuration = TimeSpan.Zero;
SongPosition = TimeSpan.Zero;
SongProgress = TimeSpan.Zero;
}
}
10 replies