首页 > 解决方案 > 如何与 Spotify 服务器同步时钟?

问题描述

我正在尝试使用 Spotify Web API(https://developer.spotify.com/documentation/web-api/reference/)为音乐创建很酷的可视化。

我正在尝试的是首先获取用户正在播放的内容,他们的进度,然后我也获取曲目分析。

可以在此端点上获取当前播放的歌曲:https ://developer.spotify.com/documentation/web-api/reference/player/get-the-users-currently-playing-track/

从回复中对我来说重要的东西基本上是这样的:

{
  "id": H7sbas7Gda98sh...., //ID of the song
  "timestamp": 1490252122574, //unix timestamp when the data is fetched
  "progress_ms": 42567 //The progress of the track in milliseconds
}

显然,在请求和我在应用程序中解析响应的时间之间经过了一段时间。所以计划是我以这种方式同步音乐:

const auto current_time = get_current_unix_timestamp();
const auto difference = current_time - timestamp; //the timestamp that is in the response
const offset = progress_ms + difference; //the progress_ms that is in the response

这个理论很酷,但似乎服务器和我的系统之间的时钟不同步,因为我通常会得到像 -1638 这样的值来表示差异,这显然不好,因为这意味着我解析数据的时间比获取数据的时间早。

所以我的问题是,我有哪些选项可以将我的时钟同步到 Spotify 服务器?如果不可能,我必须有哪些选项才能正确同步音乐?我在 Spotify 文档中找不到任何内容,尽管它应该是可能的,因为已经有一些现有的应用程序可以做我想做的事情(例如:https ://www.kaleidosync.com/ )

标签: spotifyclocksystem-clock

解决方案


似乎同步目前不实用,因为虽然文档说“时间戳”字段表示 API 何时为您提供数据,但由于他们方面的一些问题,它实际上并没有按照它所说的那样做:https://github .com/spotify/web-api/issues/1073

相反,时间戳似乎只有在有新歌开始、暂停或搜索时才会改变。这意味着我们无法知道 API 响应是何时生成的。


推荐阅读