spotify - 如何与 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/ )
解决方案
似乎同步目前不实用,因为虽然文档说“时间戳”字段表示 API 何时为您提供数据,但由于他们方面的一些问题,它实际上并没有按照它所说的那样做:https://github .com/spotify/web-api/issues/1073
相反,时间戳似乎只有在有新歌开始、暂停或搜索时才会改变。这意味着我们无法知道 API 响应是何时生成的。
推荐阅读
- c - 从二进制文件中读取 UTF-8 字符的简单方法?
- php - 我正在尝试通过不断收到错误“未定义的变量:代码”来制作电子邮件验证系统
- windows - 如何获取卷 GUID 路径的 PIDL
- android - Cordova 以格式错误的 base64 字符串形式打开
- python-3.x - 重复运行函数时如何不覆盖csv中的列
- forms - 我正在尝试仅为我的待办事项列表中的字母和数字验证文本输入字段,而 React Hook Form 对我不起作用
- javascript - 为什么我不能在 HTML 中设置我的 span 标签的 innerHTML?
- python - 将变量插入字符串时如何保留换行符?
- multithreading - python中语音识别的并行处理
- c# - 在 C# Visual Studio ERCOT 上使用 Signed SOAP 发出 Web 请求