A Spotify API written in Java to access the current playing song.
There is no need for an access token, any internet connection or a premium account
because the API reads the information directly from the application itself.
- Track id
- Track title & artist
- Track progress & length
- Playing state (Playing, paused)
- Track cover
- Media keys (Previous song, play/pause & next song)
- Windows
- macOS
- Linux distros that uses systemd
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.LabyStudio:java-spotify-api:+:all'
}Create the API and get the current playing song and position:
// Create a new SpotifyAPI for your operating systemSpotifyAPIapi = SpotifyAPIFactory.createInitialized();
// It has no track until the song started playing onceif (api.hasTrack()) {
System.out.println(api.getTrack());
}
// It has no position until the song is paused, the position changed or the song changedif (api.hasPosition()) {
System.out.println(api.getPosition());
}Register a listener to get notified when the song changes:
SpotifyAPIlocalApi = SpotifyAPIFactory.create();
localApi.registerListener(newSpotifyListener() {
@OverridepublicvoidonConnect() {
System.out.println("Connected to Spotify!");
}
@OverridepublicvoidonTrackChanged(Tracktrack) {
System.out.printf("Track changed: %s (%s)\n", track, formatDuration(track.getLength()));
if (track.getCoverArt() != null) {
BufferedImagecoverArt = track.getCoverArt();
System.out.println("Track cover: " + coverArt.getWidth() + "x" + coverArt.getHeight());
}
}
@OverridepublicvoidonPositionChanged(intposition) {
if (!localApi.hasTrack()) {
return;
}
intlength = localApi.getTrack().getLength();
floatpercentage = 100.0F / length * position;
System.out.printf(
"Position changed: %s of %s (%d%%)\n",
formatDuration(position),
formatDuration(length),
(int) percentage
);
}
@OverridepublicvoidonPlayBackChanged(booleanisPlaying) {
System.out.println(isPlaying ? "Song started playing" : "Song stopped playing");
}
@OverridepublicvoidonSync() {
// System.out.println(formatDuration(api.getPosition()));
}
@OverridepublicvoidonDisconnect(Exceptionexception) {
System.out.println("Disconnected: " + exception.getMessage());
// api.stop();
}
});
// Initialize the APIlocalApi.initialize();Request information of any track id using open.spotify.com:
// Create a secret providerSecretProvidersecretProvider = newDefaultSecretProvider(
// Note: You have to update the secret with the latest TOTP secret from open.spotify.com// or find a way to retrieve it automatically from their websiteSecret.fromString("=n:b#OuEfH\fE])e*K", 10)
);
// Create an instance of the Open Spotify APIOpenSpotifyAPIopenSpotifyAPI = newOpenSpotifyAPI(secretProvider);
// Fetch cover art image by track idBufferedImagecoverArt = openSpotifyAPI.requestImage(trackId);
// Fetch track information by track idOpenTrackopenTrack = openSpotifyAPI.requestOpenTrack(trackId);You can also skip the current song using the Media Key API:
SpotifyAPIapi = SpotifyAPIFactory.createInitialized();
// Send media key to the operating systemapi.pressMediaKey(MediaKey.NEXT);