MCPcopy Index your code
hub / github.com/LabyStudio/java-spotify-api

github.com/LabyStudio/java-spotify-api @1.5.2

Chat with this repo
repository ↗ · DeepWiki ↗ · release 1.5.2 ↗ · + Follow
411 symbols 834 edges 57 files 141 documented · 34%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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
  • Linux distros that uses systemd

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 system
SpotifyAPI api = SpotifyAPIFactory.createInitialized();

// It has no track until the song started playing once
if (api.hasTrack()) {
    System.out.println(api.getTrack());
}

// It has no position until the song is paused, the position changed or the song changed
if (api.hasPosition()) {
    System.out.println(api.getPosition());
}

Register a listener to get notified when the song changes:

SpotifyAPI localApi = SpotifyAPIFactory.create();
localApi.registerListener(new SpotifyListener() {
    @Override
    public void onConnect() {
        System.out.println("Connected to Spotify!");
    }

    @Override
    public void onTrackChanged(Track track) {
        System.out.printf("Track changed: %s (%s)\n", track, formatDuration(track.getLength()));

        if (track.getCoverArt() != null) {
            BufferedImage coverArt = track.getCoverArt();
            System.out.println("Track cover: " + coverArt.getWidth() + "x" + coverArt.getHeight());
        }
    }

    @Override
    public void onPositionChanged(int position) {
        if (!localApi.hasTrack()) {
            return;
        }

        int length = localApi.getTrack().getLength();
        float percentage = 100.0F / length * position;

        System.out.printf(
                "Position changed: %s of %s (%d%%)\n",
                formatDuration(position),
                formatDuration(length),
                (int) percentage
        );
    }

    @Override
    public void onPlayBackChanged(boolean isPlaying) {
        System.out.println(isPlaying ? "Song started playing" : "Song stopped playing");
    }

    @Override
    public void onSync() {
        // System.out.println(formatDuration(api.getPosition()));
    }

    @Override
    public void onDisconnect(Exception exception) {
        System.out.println("Disconnected: " + exception.getMessage());

        // api.stop();
    }
});

// Initialize the API
localApi.initialize();

Request information of any track id using open.spotify.com:

// Create a secret provider
SecretProvider secretProvider = new DefaultSecretProvider(
        // 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 website
        Secret.fromString("=n:b#OuEfH\fE])e*K", 10)
);

// Create an instance of the Open Spotify API
OpenSpotifyAPI openSpotifyAPI = new OpenSpotifyAPI(secretProvider);

// Fetch cover art image by track id
BufferedImage coverArt = openSpotifyAPI.requestImage(trackId);

// Fetch track information by track id
OpenTrack openTrack = openSpotifyAPI.requestOpenTrack(trackId);

You can also skip the current song using the Media Key API:

SpotifyAPI api = SpotifyAPIFactory.createInitialized();

// Send media key to the operating system
api.pressMediaKey(MediaKey.NEXT);

Extension points exported contracts — how you extend this code

SpotifyListener (Interface)
Interface to receive playback updates from Spotify. @author LabyStudio [4 implementers]
src/main/java/de/labystudio/spotifyapi/SpotifyListener.java
SecretProvider (Interface)
This interface is used to provide a secret for TOTP generation. It must be implemented to retrieve the latest secret fro [2 …
src/main/java/de/labystudio/spotifyapi/open/totp/provider/SecretProvider.java
SpotifyAPI (Interface)
This is the main interface for the SpotifyAPI. It is used to get the current track and the current position of a song. T [1 …
src/main/java/de/labystudio/spotifyapi/SpotifyAPI.java
WinApi (Interface)
Windows API Utilities @author LabyStudio [1 implementers]
src/main/java/de/labystudio/spotifyapi/platform/windows/api/WinApi.java
SearchCondition (Interface)
Search condition function to check if the address matches additional criteria.
src/main/java/de/labystudio/spotifyapi/platform/windows/api/WinProcess.java

Core symbols most depended-on inside this repo

get
called by 24
src/main/java/de/labystudio/spotifyapi/open/Cache.java
getValue
called by 16
src/main/java/de/labystudio/spotifyapi/platform/linux/api/model/Variant.java
equals
called by 15
src/main/java/de/labystudio/spotifyapi/model/Track.java
toString
called by 11
src/main/java/de/labystudio/spotifyapi/platform/linux/api/model/Variant.java
execute
called by 9
src/main/java/de/labystudio/spotifyapi/platform/osx/api/AppleScript.java
get
called by 9
src/main/java/de/labystudio/spotifyapi/platform/linux/api/DBusSend.java
get_spotify_session
called by 8
rust/src/session.rs
getId
called by 7
src/main/java/de/labystudio/spotifyapi/model/Track.java

Shape

Method 323
Class 62
Interface 13
Function 12
Enum 1

Languages

Java97%
Rust3%

Modules by API surface

src/main/java/de/labystudio/spotifyapi/platform/windows/api/WinProcess.java32 symbols
src/main/java/de/labystudio/spotifyapi/open/OpenSpotifyAPI.java17 symbols
src/main/java/de/labystudio/spotifyapi/SpotifyAPI.java16 symbols
src/main/java/de/labystudio/spotifyapi/open/model/track/OpenTrack.java14 symbols
src/main/java/de/labystudio/spotifyapi/platform/windows/api/WinApi.java13 symbols
src/main/java/de/labystudio/spotifyapi/platform/windows/api/playback/PlaybackAccessor.java12 symbols
src/main/java/de/labystudio/spotifyapi/platform/windows/WinSpotifyAPI.java12 symbols
src/main/java/de/labystudio/spotifyapi/model/Track.java12 symbols
src/main/java/de/labystudio/spotifyapi/platform/windows/api/playback/source/MediaControlPlaybackAccessor.java11 symbols
src/main/java/de/labystudio/spotifyapi/platform/windows/api/playback/source/LegacyPlaybackAccessor.java11 symbols
src/main/java/de/labystudio/spotifyapi/platform/windows/api/jna/WindowsMediaControl.java11 symbols
src/main/java/de/labystudio/spotifyapi/platform/windows/api/jna/Tlhelp32.java11 symbols

For agents

$ claude mcp add java-spotify-api \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact