diff --git a/PlayWallNative/src/de/tobias/playpad/NativeAudioMacHandler.java b/PlayWallNative/src/de/tobias/playpad/NativeAudioMacHandler.java index 07fa6b9f8de81d77b8ee7334f0c64a92f822ce32..d77d73765e81f50ba79e4d3a44b1e3ce7a408901 100644 --- a/PlayWallNative/src/de/tobias/playpad/NativeAudioMacHandler.java +++ b/PlayWallNative/src/de/tobias/playpad/NativeAudioMacHandler.java @@ -1,11 +1,16 @@ package de.tobias.playpad; import java.nio.file.Path; +import java.util.ArrayList; +import java.util.ConcurrentModificationException; +import java.util.Iterator; +import java.util.List; import de.tobias.playpad.audio.AudioHandler; import de.tobias.playpad.pad.PadStatus; import de.tobias.playpad.pad.conntent.PadContent; import de.tobias.utils.util.Worker; +import javafx.application.Platform; import javafx.beans.property.ObjectProperty; import javafx.beans.property.ReadOnlyObjectProperty; import javafx.beans.property.SimpleObjectProperty; @@ -13,6 +18,8 @@ import javafx.util.Duration; public class NativeAudioMacHandler extends AudioHandler { + private static final int SLEEP_TIME_POSITION = 100; + private static int counter = 0; private final int id; @@ -20,6 +27,39 @@ public class NativeAudioMacHandler extends AudioHandler { private ObjectProperty<Duration> durationProperty; private boolean isLoaded; + private static Thread positionThread; + private static List<NativeAudioMacHandler> playedHandlers = new ArrayList<>(); + + static { + positionThread = new Thread(() -> + { + while (true) { + try { + if (playedHandlers.isEmpty()) { + synchronized (positionThread) { + positionThread.wait(); + } + } + + for (Iterator<NativeAudioMacHandler> iterator = playedHandlers.iterator(); iterator.hasNext();) { + NativeAudioMacHandler handler = iterator.next(); + Duration seconds = Duration.seconds(NativeAudio.getPosition(handler.id)); + + Platform.runLater(() -> handler.positionProperty.set(seconds)); + } + + Thread.sleep(SLEEP_TIME_POSITION); + } catch (InterruptedException e) { + } catch (ConcurrentModificationException e) { + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + + positionThread.start(); + } + public NativeAudioMacHandler(PadContent content) { super(content); @@ -31,16 +71,33 @@ public class NativeAudioMacHandler extends AudioHandler { @Override public void play() { NativeAudio.play(id); + + boolean start = false; + if (playedHandlers.isEmpty()) { + start = true; + } + + if (!playedHandlers.contains(this)) + playedHandlers.add(this); + if (start) { + synchronized (positionThread) { + positionThread.notify(); + } + } } @Override public void pause() { NativeAudio.pause(id); + if (playedHandlers.contains(this)) + playedHandlers.remove(this); } @Override public void stop() { NativeAudio.stop(id); + if (playedHandlers.contains(this)) + playedHandlers.remove(this); } @Override @@ -77,12 +134,24 @@ public class NativeAudioMacHandler extends AudioHandler { @Override public void loadMedia(Path[] paths) { + Platform.runLater(() -> + { + if (getContent().getPad().isPadVisible()) { + getContent().getPad().getController().getView().showBusyView(true); + } + }); Worker.runLater(() -> { isLoaded = NativeAudio.load(id, paths[0].toString().replace(" ", "%20")); if (isLoaded) { - durationProperty.set(Duration.seconds(NativeAudio.getDuration(id))); - getContent().getPad().setStatus(PadStatus.READY); + Platform.runLater(() -> + { + durationProperty.set(Duration.seconds(NativeAudio.getDuration(id))); + getContent().getPad().setStatus(PadStatus.READY); + if (getContent().getPad().isPadVisible()) { + getContent().getPad().getController().getView().showBusyView(false); + } + }); } }); }