Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Spotify/src/bg/sofia/uni/fmi/mjt/client/AudioClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
16 changes: 11 additions & 5 deletions Spotify/src/bg/sofia/uni/fmi/mjt/client/SpotifyClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -26,7 +30,9 @@ public static void main(String[] args) {
Scanner scanner = new Scanner(System.in)) {

Thread.currentThread().setName("Client thread " + socket.getLocalPort());


BlockingQueue<String> commandQueue = new ArrayBlockingQueue<>(10);
ExecutorService executorService = Executors.newFixedThreadPool(2);
AudioClient audioClient = new AudioClient();

while (true) {
Expand All @@ -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);
}
}

Expand Down
22 changes: 0 additions & 22 deletions Spotify/src/bg/sofia/uni/fmi/mjt/server/track/Track.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,36 +58,14 @@ public void play() {
throw new RuntimeException(e);
}
});

audioThread.start();

try {
audioThread.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}

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;
}
Expand Down