StreamingPlayer.java

package ui.ultimate64;

import sidplay.player.ObjectProperty;
import sidplay.player.State;

import javax.sound.sampled.LineUnavailableException;
import java.io.IOException;

import static sidplay.player.State.*;

public abstract class StreamingPlayer {
    private static final int QUIT_MAX_WAIT_TIME = 1000;

    private final ObjectProperty<State> stateProperty = new ObjectProperty<>(State.class.getSimpleName(), QUIT);

    private Thread streamingThread;

    private final Runnable streamingRunnable = () -> {
        try {
            open();
            stateProperty.set(START);
            stateProperty.set(PLAY);
            while (stateProperty.get() == State.PLAY) {
                play();
            }
        } catch (InterruptedException | IOException | LineUnavailableException e) {
            throw new RuntimeException(e.getMessage(), e);
        } finally {
            close();
        }
    };

    protected abstract void open() throws IOException, LineUnavailableException, InterruptedException;

    protected abstract void play() throws IOException, InterruptedException;

    protected abstract void close();

    public void start() {
        if (streamingThread == null || !streamingThread.isAlive()) {
            streamingThread = new Thread(streamingRunnable, "Player");
            streamingThread.setPriority(Thread.MAX_PRIORITY);
            streamingThread.start();
        }
    }

    public void stop() {
        try {
            while (streamingThread != null && streamingThread.isAlive()) {
                stateProperty.set(QUIT);
                streamingThread.join(QUIT_MAX_WAIT_TIME);
            }
        } catch (InterruptedException ignored) {
        }
    }
}