使用 Spotify Web API 和 Web Playback SDK,有人知道检测曲目播放何时完成(即歌曲结束)的好方法吗?
我知道一些名为player_state_changed
在Web Playback SDK中,但是事件中提供的数据似乎对我没有太大帮助。
在我的网络应用程序中,下一首要播放的歌曲是自发确定的。如果我只能在当前曲目结束时确定它,那就太好了。因此,没有播放列表。相反,该应用程序会连续播放单个曲目,并应在当前歌曲结束后确定下一首歌曲。
同时我在这里尝试使用以下代码:
var bCallingNextSong = false;
player.addListener('player_state_changed', state => {
if (state != null &&
state.position == 0 &&
state.duration == 0 &&
state.paused == true) {
if (!bCallingNextSong) {
playNextSong();
}
}
});
function playNextSong() {
$.ajax({
url:"playback.php", //the page containing php script
type: "post", //request type,
dataType: 'json',
data: {action: "next", device: deviceId},
success: function(result) {
bCallingNextSong = false;
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
bCallingNextSong = false;
}
});
}
and in playback.php
:
switch ($action) {
case "next":
//some blackbox magic here to determine next song, do logging etc.
//but for now just play the same song over and over
$api->play($deviceId, ['uris' => "spotify:track:0eGsygTp906u18L0Oimnem"],]);
echo json_encode(array("answer"=>"played next"));
break;
// more code
}
然而,这经常(但并非总是)抛出一个 InvalidStateError (我还没有找到确切的位置和原因)。堆栈显示 dequeueUpdates / tryUpdate / _processUpdate / _appendUpdate。然而,我是一个初学者,我必须先学习如何使用 Firefox 调试器..:)