LabyStudio / java-spotify-api

Spotify API written in Java to locally access the current song information
15 stars 5 forks source link

onPositionChanged and onPlaybackChanged not updating #2

Open AquaCobell opened 1 year ago

AquaCobell commented 1 year ago

After using most of the documentation code:

Main.java:

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");

        SpotifyController controller = new SpotifyController("http://192.168.188.74/api/custom?name=Spotify");

        controller.register();

    }
}

SpotifyController.java

import de.labystudio.spotifyapi.SpotifyAPI;
import de.labystudio.spotifyapi.SpotifyAPIFactory;
import de.labystudio.spotifyapi.SpotifyListener;
import de.labystudio.spotifyapi.model.Track;

public class SpotifyController 
{   

    private SpotifyAPI spotify;
    Httpcontroller httpcontroller;

    SpotifyController(String url)
    {
        spotify = SpotifyAPIFactory.create();
        httpcontroller = new Httpcontroller("http://192.168.188.74/api/custom?name=Spotify");
    }

    public void register()
    {
        spotify.registerListener(new SpotifyListener() 
        {

            @Override
            public void onConnect() {
                System.out.println("Connected to Spotify!");
            }

            @Override
            public void onTrackChanged(Track track) {
                System.out.println("Track changed:" + track);
            }

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

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

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

                System.out.println("Position change: " + position);
            }

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

            @Override
            public void onSync() {

            }

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

                spotify.stop();
            }

        });

        spotify.initialize();
    }

}

Ive noticed it never prints "Song stopped playing" and "Position change" during songs, it only prints position change after skipping a song. Am I missing something or is this a bug?

LabyStudio commented 1 year ago

It basically means it can't find the memory address of your track position. It works for me, and since I can't look into your memory to figure out why it fails on your machine, it's really difficult to analyze the issue.

You can verify the issue by setting the VM option -DSPOTIFY_API_DEBUG to enable debug logging.

Have you tried to restart Spotify and change playlists/songs?

HerXayah commented 1 year ago

@AquaCobell are you by chance using Spotify via Scoop or any other Package manager?

AquaCobell commented 11 months ago

@PrincessAkira no im using the jar file

AquaCobell commented 11 months ago

@LabyStudio thanks i'll look into it. Is it supposed to update every second or only when i manually forward in spotify itself?

NULLYUKI commented 11 months ago

@PrincessAkira no im using the jar file

I think you misunderstood the question. Did you installed Spotify via Scoop or any other Package manager? Or did you manually downloaded Spotify from the Spotify website?

AquaCobell commented 11 months ago

@NULLYUKI ah sorry you're right, I got it from their website via the installer