我一次连接到Spotify遥控器,但是好像我在调用remote.getPlayerApi().skipNext()
,remote.getPlayerApi().skipPrevious()
或remote.getPlayerApi.play(uri)
时同时使用3-4个遥控器。做出上述远程命令后,remote.getPlayerApi().subscriptionToPlayerState()
被快速连续调用3-4次。
但是,暂停和继续可以正常工作。请注意,所有这些都在前台服务上运行。
我尝试重新启动手机,但是发生了同样的事情。就像旧的遥控器没有正确断开连接,连接并执行订阅回调一样。
I basically did everything here供参考,以及代码的我的一面:
// Set player state subscription to Spotify Remote SDK
private void setupSpotifySubscriptions() {
mSpotifyAppRemote.getPlayerApi().subscribeToPlayerState().setEventCallback(new Subscription.EventCallback<PlayerState>() {
@Override
public void onEvent(PlayerState playerState) {
updatePlayerState(playerState);
}
});
}
// Update the player UI in Flutter
private void updatePlayerState(PlayerState playerState) {
HashMap<String, Object> payloadToServer = new HashMap<>();
payloadToServer.put("position", playerState.playbackPosition);
payloadToServer.put("duration", playerState.track.duration);
payloadToServer.put("song_name", playerState.track.name);
payloadToServer.put("song_artists", stringifyArtists(playerState.track.artists));
payloadToServer.put("song_id", playerState.track.uri);
payloadToServer.put("is_paused", playerState.isPaused);
socket.emitToServer("player_state", payloadToServer);
}
一旦我连接,就会出现来自Spotify远程的3-4条消息,这使我的假设看起来很合理:
D/RemoteServiceIo(32686): Connecting to Spotify service
D/RemoteServiceIo(32686): Message from Spotify: [2,4,{"authid":null,"authmethod":null,"authprovider":null,"authrole":null,"roles":{"broker":{},"caller":null,"dealer":{},"subscriber":null}}]
D/RemoteServiceIo(32686): Message from Spotify: [33,1,8]
D/RemoteServiceIo(32686): Message from Spotify: [36,8,28,{},[],{"long_text":"","short_text":"","code":0}]
D/MainActivity(32686): Connected! Yay!
D/RemoteServiceIo(32686): Message from Spotify: [33,2,9]
D/RemoteServiceIo(32686): Message from Spotify: [36,9,29,{},[],{"track":{"artist":{"name":"Jesper Ryom","type":"artist","uri":"spotify:artist:6QAXPFWafsrhltnhogrQ1P"},"artists":[{"name":"Jesper Ryom","type":"artist","uri":"spotify:artist:6QAXPFWafsrhltnhogrQ1P"}],"album":{"name":"Syvsover","type":"album","uri":"spotify:album:3Se55Em2mSqYwuQLqS7XDb"},"saved":false,"duration_ms":423586,"name":"Clock","uri":"spotify:track:6N6jCOPR4kEczFBMrgun5A","image_id":"content://com.spotify.music/image/Njc5YmIzMzYyYmM5YjhlODM3ZjU3NWQzODQ4Y2I0M2JkZTVjNmM5ZQ%3D%3D%0A","is_episode":false,"is_podcast":false},"is_paused":true,"playback_speed":0.0,"playback_position":67667,"playback_options":{"shuffle":false,"repeat":0},"playback_restrictions":{"can_skip_next":true,"can_skip_prev":false,"can_repeat_track":false,"can_repeat_context":false,"can_toggle_shuffle":false,"can_seek":true}}]
更新:
使用信号量的临时修复。但是,解决此问题仍然很高兴。
private void updatePlayerState(PlayerState playerState) throws InterruptedException {
HashMap<String, Object> payloadToServer = new HashMap<>();
payloadToServer.put("position", playerState.playbackPosition);
payloadToServer.put("duration", playerState.track.duration);
payloadToServer.put("song_name", playerState.track.name);
payloadToServer.put("song_artists", stringifyArtists(playerState.track.artists));
payloadToServer.put("song_id", playerState.track.uri);
payloadToServer.put("is_paused", playerState.isPaused);
if (available.tryAcquire()) {
socket.emitToServer("player_state", payloadToServer);
}
}
一旦收到套接字响应:
private Emitter.Listener onPlayerState = new Emitter.Listener() {
@Override
public void call(Object... args) {
try {
System.out.println("[onPlayerState] " + args[0].toString());
HashMap<String, Object> playerState = new ObjectMapper().readValue(args[0].toString(), HashMap.class);
methodChannel.invokeMethod(SPOTIFY + PLAYER_STATE, playerState);
if(player_state.get("song_id") != playerState.get("song_id")){
methodChannel.invokeMethod(POP_USER_QUEUE, playerState.get("song_id"));
popUserQueue((String) playerState.get("song_id"));
}
player_state = playerState;
releaseSemaphore();
} catch (java.lang.Exception e) {
System.out.println(e.toString());
}
}
};
private void releaseSemaphore(){
new Thread(new Runnable() {
public void run() {
try {
TimeUnit.MILLISECONDS.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
available.release();
}
}).start();
}