Android Spotify SDK批量添加歌曲至队列失败求助
Hey there, let's break down why your playback stops when trying to add tracks to the queue after the first one. I've run into similar quirks with the Spotify Android SDK, so here are the most likely fixes:
1. You're using the wrong method for appending to the queue
The queue() method isn't for adding tracks to an already-running playback session — it's intended to launch a new queue with the specified track. When you call it mid-playback, it replaces your current queue and tries to switch to the new track, which often fails silently (especially with a null callback) and stops playback entirely.
Instead, use addToQueue() to append tracks to the end of your existing queue. Here's how to structure that call properly:
spotifyPlayer.addToQueue(trackUriList.get(loopInt), new OperationCallback() { @Override public void onSuccess() { Log.d("SpotifyQueue", "Track added to queue successfully!"); } @Override public void onError(Error error) { Log.e("SpotifyQueue", "Failed to add track: " + error.getMessage() + " (Error code: " + error.getCode() + ")"); } });
2. Null callback is hiding critical error details
Passing null as the OperationCallback means you're missing out on error logs that would tell you exactly why the queue operation is failing. Even valid URIs can run into issues like:
- The player not being in the
READYstate when you call the method - Temporary network issues blocking track metadata loading
- Unexpected session state changes
Always include a callback to capture these errors — it's the fastest way to diagnose what's going wrong.
3. Timing: Wait for the player to be fully ready
Make sure you only start adding tracks to the queue after the onPlayerReady() callback fires. If you try to modify the queue before the player is initialized, the operation will fail and could interrupt playback of the first track.
Double-check that your loop to add tracks is triggered after you get the ready signal from the Spotify player.
4. Double-check your URI format
Even if URIs look correct in logs, confirm they use the proper Spotify URI scheme: spotify:track:YOUR_TRACK_ID. HTTP URLs (like https://open.spotify.com/track/...) won't work for queue operations — the SDK requires the native URI format.
Quick Fix Summary
Swap queue() for addToQueue(), add an error callback to debug issues, and ensure you're only modifying the queue once the player is fully ready. That should keep your first track playing while seamlessly adding the rest to the queue.
内容的提问来源于stack exchange,提问作者Joshua Finks

