diff --git a/Spotify/src/bg/sofia/uni/fmi/mjt/client/AudioClient.java b/Spotify/src/bg/sofia/uni/fmi/mjt/client/AudioClient.java index 2713e0d..a627af7 100644 --- a/Spotify/src/bg/sofia/uni/fmi/mjt/client/AudioClient.java +++ b/Spotify/src/bg/sofia/uni/fmi/mjt/client/AudioClient.java @@ -27,17 +27,25 @@ public void playSong(String songPath) { while (playing && (bytesRead = audioInputStream.read(buffer, 0, buffer.length)) != -1) { sourceDataLine.write(buffer, 0, bytesRead); + if(!playing){ + throw new RuntimeException("Song stopped"); + } } sourceDataLine.drain(); - } catch (IOException | UnsupportedAudioFileException | LineUnavailableException e) { + } catch (IOException | UnsupportedAudioFileException | LineUnavailableException | RuntimeException e) { throw new RuntimeException(e); + } finally { + if (sourceDataLine != null && sourceDataLine.isOpen()) { + sourceDataLine.stop(); + sourceDataLine.close(); + } } } public void stopSong() { + playing = false; if (sourceDataLine != null && sourceDataLine.isOpen()) { - playing = false; sourceDataLine.stop(); sourceDataLine.close(); } diff --git a/Spotify/src/bg/sofia/uni/fmi/mjt/client/SpotifyClient.java b/Spotify/src/bg/sofia/uni/fmi/mjt/client/SpotifyClient.java index 2ca01ed..4e3ab08 100644 --- a/Spotify/src/bg/sofia/uni/fmi/mjt/client/SpotifyClient.java +++ b/Spotify/src/bg/sofia/uni/fmi/mjt/client/SpotifyClient.java @@ -7,6 +7,10 @@ import java.io.UncheckedIOException; import java.net.Socket; import java.util.Scanner; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import static bg.sofia.uni.fmi.mjt.server.SpotifyServer.logException; @@ -26,7 +30,9 @@ public static void main(String[] args) { Scanner scanner = new Scanner(System.in)) { Thread.currentThread().setName("Client thread " + socket.getLocalPort()); - + + BlockingQueue commandQueue = new ArrayBlockingQueue<>(10); + ExecutorService executorService = Executors.newFixedThreadPool(2); AudioClient audioClient = new AudioClient(); while (true) { @@ -36,16 +42,16 @@ public static void main(String[] args) { System.out.println("Disconnected from the server"); break; } - if (message.startsWith(STOP_COMMAND)) { - audioClient.stopSong(); - } + writer.println(message); String reply = reader.readLine(); System.out.println(reply); if (message.startsWith(PLAY_COMMAND)) { - audioClient.playSong(WAV_PATH+"Smooth_Sade.wav"); + executorService.execute(() -> audioClient.playSong(WAV_PATH + "Smooth_Sade.wav")); + } else if (message.startsWith(STOP_COMMAND)) { + executorService.execute(audioClient::stopSong); } } diff --git a/Spotify/src/bg/sofia/uni/fmi/mjt/server/track/Track.java b/Spotify/src/bg/sofia/uni/fmi/mjt/server/track/Track.java index d4a9c5a..4d882c7 100644 --- a/Spotify/src/bg/sofia/uni/fmi/mjt/server/track/Track.java +++ b/Spotify/src/bg/sofia/uni/fmi/mjt/server/track/Track.java @@ -58,9 +58,7 @@ public void play() { throw new RuntimeException(e); } }); - audioThread.start(); - try { audioThread.join(); } catch (InterruptedException e) { @@ -68,26 +66,6 @@ public void play() { } } - private void playAudio(AudioInputStream audioInputStream) { - AudioFormat audioFormat = audioInputStream.getFormat(); - DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat); - try (SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo)) { - sourceDataLine.open(audioFormat); - sourceDataLine.start(); - byte[] buffer = new byte[BYTES]; - int bytesRead; - while (!stopRequested && (bytesRead = audioInputStream.read(buffer, 0, buffer.length)) != -1) { - sourceDataLine.write(buffer, 0, bytesRead); - } - sourceDataLine.drain(); - } catch (LineUnavailableException | IOException e) { - logException(e); - throw new InvalidAudioFileException("Error while playing audio", e); - } finally { - stopRequested = false; - } - } - public void stop() { stopRequested = true; }