Skip to content

Commit 45e4003

Browse files
committed
implement api stop function, improve demo code, version 1.0.1
1 parent af6369f commit 45e4003

File tree

6 files changed

+66
-18
lines changed

6 files changed

+66
-18
lines changed

README.md

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,42 +37,50 @@ if (api.hasPosition()) {
3737

3838
Register a listener to get notified when the song changes:
3939
```java
40+
SpotifyAPI api = SpotifyAPIFactory.create();
4041
api.registerListener(new SpotifyListener() {
4142
@Override
4243
public void onConnect() {
4344
System.out.println("Connected to Spotify!");
4445
}
45-
46+
4647
@Override
4748
public void onTrackChanged(Track track) {
48-
System.out.println("Track changed: [" + track.getId() + "] " + track.getName() + " - " + track.getArtist() + " (" + formatDuration(track.getLength()) + ")");
49+
System.out.printf("Track changed: %s (%s)\n", track, formatDuration(track.getLength()));
4950
}
50-
51+
5152
@Override
5253
public void onPositionChanged(int position) {
53-
if (api.getTrack() == null) {
54+
if (!api.hasTrack()) {
5455
return;
5556
}
56-
57+
5758
int length = api.getTrack().getLength();
5859
float percentage = 100.0F / length * position;
59-
60-
System.out.println("Seek: " + (int) percentage + "% (" + formatDuration(position) + " / " + formatDuration(length) + ")");
60+
61+
System.out.printf(
62+
"Position changed: %s of %s (%d%%)\n",
63+
formatDuration(position),
64+
formatDuration(length),
65+
(int) percentage
66+
);
6167
}
62-
68+
6369
@Override
6470
public void onPlayBackChanged(boolean isPlaying) {
65-
System.out.println(isPlaying ? "Playing" : "Paused");
71+
System.out.println(isPlaying ? "Song started playing" : "Song stopped playing");
6672
}
67-
73+
6874
@Override
6975
public void onSync() {
70-
76+
7177
}
72-
78+
7379
@Override
7480
public void onDisconnect(Exception exception) {
7581
System.out.println("Disconnected: " + exception.getMessage());
82+
83+
// api.stop();
7684
}
7785
});
7886
```

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group 'de.labystudio'
7-
version '1.0.0'
7+
version '1.0.1'
88

99
compileJava {
1010
sourceCompatibility = '1.8'

src/main/java/de/labystudio/spotifyapi/SpotifyAPI.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,10 @@ default boolean hasTrack() {
7272
* @param listener the listener to register
7373
*/
7474
void registerListener(SpotifyListener listener);
75+
76+
/**
77+
* Disconnect from the Spotify application and stop all background tasks.
78+
* It will also remove all listeners.
79+
*/
80+
void stop();
7581
}

src/main/java/de/labystudio/spotifyapi/platform/AbstractSpotifyAPI.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ public void registerListener(SpotifyListener listener) {
2323
this.listeners.add(listener);
2424
}
2525

26+
@Override
27+
public void stop() {
28+
this.listeners.clear();
29+
}
2630
}

src/main/java/de/labystudio/spotifyapi/platform/windows/WinSpotifyAPI.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import java.util.Objects;
1212
import java.util.concurrent.Executors;
13+
import java.util.concurrent.ScheduledFuture;
1314

1415
/**
1516
* Windows implementation of the SpotifyAPI.
@@ -29,18 +30,25 @@ public class WinSpotifyAPI extends AbstractSpotifyAPI {
2930
private long lastTimePositionUpdated;
3031
private boolean positionKnown = false;
3132

33+
private ScheduledFuture<?> task;
34+
3235
/**
3336
* Initialize the SpotifyAPI Windows implementation.
3437
* It will create a task that will update the current track and position every second.
3538
*
3639
* @return the initialized SpotifyAPI
40+
* @throws IllegalStateException if the API is already initialized
3741
*/
3842
public SpotifyAPI initialize() {
43+
if (this.task != null) {
44+
throw new IllegalStateException("The SpotifyAPI is already initialized");
45+
}
46+
3947
// Initial tick
4048
this.onTick();
4149

4250
// Start task to update every second
43-
Executors.newScheduledThreadPool(1).scheduleWithFixedDelay(
51+
this.task = Executors.newScheduledThreadPool(1).scheduleWithFixedDelay(
4452
this::onTick, 1, 1, java.util.concurrent.TimeUnit.SECONDS
4553
);
4654

@@ -150,6 +158,21 @@ public boolean isConnected() {
150158
return this.process != null && this.process.isOpen();
151159
}
152160

161+
@Override
162+
public void stop() {
163+
super.stop();
164+
165+
if (this.task != null) {
166+
this.task.cancel(true);
167+
this.task = null;
168+
}
169+
170+
if (this.process != null) {
171+
this.process.close();
172+
this.process = null;
173+
}
174+
}
175+
153176
/**
154177
* Checks if the given track ID is valid.
155178
* A track ID is valid if there are no characters with a value of zero.

src/test/java/SpotifyTest.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,29 @@ public void onConnect() {
2929

3030
@Override
3131
public void onTrackChanged(Track track) {
32-
System.out.println("Track changed: [" + track.getId() + "] " + track.getName() + " - " + track.getArtist() + " (" + formatDuration(track.getLength()) + ")");
32+
System.out.printf("Track changed: %s (%s)\n", track, formatDuration(track.getLength()));
3333
}
3434

3535
@Override
3636
public void onPositionChanged(int position) {
37-
if (api.getTrack() == null) {
37+
if (!api.hasTrack()) {
3838
return;
3939
}
4040

4141
int length = api.getTrack().getLength();
4242
float percentage = 100.0F / length * position;
4343

44-
System.out.println("Seek: " + (int) percentage + "% (" + formatDuration(position) + " / " + formatDuration(length) + ")");
44+
System.out.printf(
45+
"Position changed: %s of %s (%d%%)\n",
46+
formatDuration(position),
47+
formatDuration(length),
48+
(int) percentage
49+
);
4550
}
4651

4752
@Override
4853
public void onPlayBackChanged(boolean isPlaying) {
49-
System.out.println(isPlaying ? "Playing" : "Paused");
54+
System.out.println(isPlaying ? "Song started playing" : "Song stopped playing");
5055
}
5156

5257
@Override
@@ -57,6 +62,8 @@ public void onSync() {
5762
@Override
5863
public void onDisconnect(Exception exception) {
5964
System.out.println("Disconnected: " + exception.getMessage());
65+
66+
// api.stop();
6067
}
6168
});
6269
}

0 commit comments

Comments
 (0)