@@ -161,12 +161,14 @@ public void setStage(Stage stage) {
161161 if (this .serverDiscoveryService != null ) {
162162 this .serverDiscoveryService .stop ();
163163 this .logger .info ("Stopped Discovery Service!" );
164- if (this .musicStreamController != null && this .musicStreamController instanceof Closeable ) {
165- try {
166- ((Closeable )this .musicStreamController ).close ();
167- } catch (IOException e ) {
168- this .logger .error ("Could not close the StreamController" , e );
169- }
164+
165+ }
166+
167+ if (this .musicStreamController != null && this .musicStreamController instanceof Closeable ) {
168+ try {
169+ ((Closeable )this .musicStreamController ).close ();
170+ } catch (IOException e ) {
171+ this .logger .error ("Could not close the StreamController" , e );
170172 }
171173 }
172174
@@ -177,6 +179,14 @@ public void setStage(Stage stage) {
177179 this .logger .error ("Could not close TCP Server" , e );
178180 }
179181 }
182+
183+ if (this .clientController != null ) {
184+ try {
185+ this .clientController .close ();
186+ } catch (IOException e ) {
187+ this .logger .error ("Could not close client controller" , e );
188+ }
189+ }
180190 });
181191 }
182192 }
@@ -206,9 +216,22 @@ public void onButtonPlayPauseClicked() {
206216
207217 if (this .mediaPlayer .isPlaying ()) {
208218 this .mediaPlayer .pause ();
219+ this .musicStreamController .stop ();
209220 } else if (this .getSelectedSong () != null ) {
210221 this .logger .info ("Trying to play: " + this .getSelectedSong ().getTitle ());
211- this .mediaPlayer .play (this .getSelectedSong (), true );
222+
223+ if (this .getSelectedSong () != null ) {
224+ // Start streaming...
225+ this .logger .info ("Streaming the new song: " + this .getSelectedSong ().getTitle ());
226+ try {
227+ this .musicStreamController .play (this .getSelectedSong ());
228+ this .mediaPlayer .play (this .getSelectedSong (), true );
229+ }
230+ catch (IOException ioException ) {
231+ this .logger .error ("Error trying to stream" , ioException );
232+ }
233+ }
234+
212235 }
213236 }
214237
@@ -251,20 +274,9 @@ public void onIsPlayingChanged() {
251274 this .buttonPlayPause .setId ("pause-button" );
252275 this .buttonPlayPause .setSelected (true );
253276
254- if (this .getSelectedSong () != null ) {
255- // Start streaming...
256- this .logger .info ("Streaming the new song: " + this .getSelectedSong ().getTitle ());
257- try {
258- this .musicStreamController .play (this .getSelectedSong ());
259- }
260- catch (IOException ioException ) {
261- this .logger .error ("Error trying to stream" , ioException );
262- }
263- }
264277 } else {
265278 this .buttonPlayPause .setId ("play-button" );
266279 this .buttonPlayPause .setSelected (false );
267- this .musicStreamController .stop ();
268280 }
269281 }
270282 //endregion
0 commit comments