Skip to content

Repository files navigation

Banner

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.

Feature Overview

  • Track id
  • Track title & artist
  • Track progress & length
  • Playing state (Playing, paused)
  • Track cover
  • Media keys (Previous song, play/pause & next song)

Supported operating systems:

  • Windows
  • macOS

Gradle Setup

repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.LabyStudio:java-spotify-api:+:all'
}

Example

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:

SpotifyAPIapi = SpotifyAPIFactory.create();
api.registerListener(newSpotifyListener() {
@OverridepublicvoidonConnect() {
System.out.println("Connected to Spotify!");
}
@OverridepublicvoidonTrackChanged(Tracktrack) {
System.out.printf("Track changed: %s (%s)\n", track, formatDuration(track.getLength()));
}
@OverridepublicvoidonPositionChanged(intposition) {
if (!api.hasTrack()) {
return;
}
intlength = api.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() {
}
@OverridepublicvoidonDisconnect(Exceptionexception) {
System.out.println("Disconnected: " + exception.getMessage());
// api.stop();
}
});
// Initialize the APIapi.initialize();

Fetch an image of the current playing track:

// Create an instance of the Open Spotify API (Or use SpotifyAPI#getOpenAPI)OpenSpotifyAPIopenSpotifyAPI = newOpenSpotifyAPI();
// Download the cover art of the current songBufferedImageimageTrackCover = openSpotifyAPI.requestImage(track);

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);

About

Spotify API written in Java to locally access the current song information

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages