From 0e6987d4c7f4ca9c263b9be08588be3115b16262 Mon Sep 17 00:00:00 2001 From: mkpaz Date: Sat, 17 Jun 2023 14:23:40 +0400 Subject: [PATCH] Fix music player can't load demo files from jrt #61 --- sampler/pom.xml | 2 +- .../page/showcase/musicplayer/MediaFile.java | 10 ++--- .../page/showcase/musicplayer/Model.java | 38 ++++++++++++++++--- .../showcase/musicplayer/PlaylistPane.java | 2 +- .../showcase/musicplayer/StartScreen.java | 4 +- sampler/src/main/java/module-info.java | 2 + 6 files changed, 44 insertions(+), 14 deletions(-) diff --git a/sampler/pom.xml b/sampler/pom.xml index 041118e..b09fe49 100755 --- a/sampler/pom.xml +++ b/sampler/pom.xml @@ -247,7 +247,7 @@ jlink - java.base,java.logging,jdk.localedata,java.desktop,java.prefs,javafx.controls,javafx.fxml,javafx.swing,javafx.web + java.base,java.logging,jdk.localedata,jdk.zipfs,java.desktop,java.prefs,javafx.controls,javafx.fxml,javafx.swing,javafx.web ${build.platformModulesDir} ${build.package.runtimeImageDir} diff --git a/sampler/src/main/java/atlantafx/sampler/page/showcase/musicplayer/MediaFile.java b/sampler/src/main/java/atlantafx/sampler/page/showcase/musicplayer/MediaFile.java index 2fa9bef..f3588ec 100644 --- a/sampler/src/main/java/atlantafx/sampler/page/showcase/musicplayer/MediaFile.java +++ b/sampler/src/main/java/atlantafx/sampler/page/showcase/musicplayer/MediaFile.java @@ -8,7 +8,7 @@ import static atlantafx.sampler.page.showcase.musicplayer.MediaFile.Metadata.NO_ import static atlantafx.sampler.page.showcase.musicplayer.Utils.copyImage; import atlantafx.sampler.Resources; -import java.io.File; +import java.nio.file.Path; import java.util.HashMap; import java.util.Map; import java.util.function.Consumer; @@ -17,7 +17,7 @@ import javafx.scene.media.Media; import javafx.scene.media.MediaPlayer; @SuppressWarnings("StringOperationCanBeSimplified") -record MediaFile(File file) { +record MediaFile(Path path) { private static final Map METADATA_CACHE = new HashMap<>(); @@ -25,7 +25,7 @@ record MediaFile(File file) { // media file metadata you have to load it to media player instance, which // is costly and that instance is not even reusable. public void readMetadata(Consumer callback) { - var media = new Media(file.toURI().toString()); + var media = new Media(path.toUri().toString()); var mediaPlayer = new MediaPlayer(media); // The media information is obtained asynchronously and so not necessarily @@ -34,7 +34,7 @@ record MediaFile(File file) { // MediaPlayer and that player has transitioned to Status.READY status. mediaPlayer.setOnReady(() -> { Map metadata = media.getMetadata(); - callback.accept(METADATA_CACHE.computeIfAbsent(file.getAbsolutePath(), k -> { + callback.accept(METADATA_CACHE.computeIfAbsent(path.toAbsolutePath().toString(), k -> { var image = getTag(metadata, "image", Image.class, null); // clone everything to make sure media player will be garbage collected return new Metadata( @@ -51,7 +51,7 @@ record MediaFile(File file) { } public Media createMedia() { - return new Media(file.toURI().toString()); + return new Media(path.toUri().toString()); } private T getTag(Map metadata, String key, Class type, T defaultValue) { diff --git a/sampler/src/main/java/atlantafx/sampler/page/showcase/musicplayer/Model.java b/sampler/src/main/java/atlantafx/sampler/page/showcase/musicplayer/Model.java index a8ba6fe..7379641 100644 --- a/sampler/src/main/java/atlantafx/sampler/page/showcase/musicplayer/Model.java +++ b/sampler/src/main/java/atlantafx/sampler/page/showcase/musicplayer/Model.java @@ -2,10 +2,16 @@ package atlantafx.sampler.page.showcase.musicplayer; +import atlantafx.sampler.Launcher; import atlantafx.sampler.Resources; -import java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.FileSystems; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; +import java.util.Map; import java.util.Objects; import javafx.beans.binding.Bindings; import javafx.beans.property.ReadOnlyBooleanProperty; @@ -18,10 +24,32 @@ import javafx.scene.paint.Color; final class Model { - private static final List DEMO_FILES = List.of( - Paths.get(Resources.getResource("media/Beat Thee.mp3")).toFile(), - Paths.get(Resources.getResource("media/Study and Relax.mp3")).toFile() - ); + private static final List DEMO_FILES = getDemoFiles(); + + private static List getDemoFiles() { + // for the runtime image as it won't create zipfs automatically + URL media = Launcher.class.getResource(Resources.MODULE_DIR + "media"); + if (media != null && media.toString().startsWith("jar")) { + try { + try (var fs = FileSystems.newFileSystem( + media.toURI(), + Map.of("create", "true") + )) { + return List.of( + fs.getPath(Resources.MODULE_DIR + "media/Beat Thee.mp3"), + fs.getPath(Resources.MODULE_DIR + "media/Study and Relax.mp3") + ); + } + } catch (URISyntaxException | IOException e) { + throw new RuntimeException(e); + } + } + + return List.of( + Paths.get(Resources.getResource("media/Beat Thee.mp3")), + Paths.get(Resources.getResource("media/Study and Relax.mp3")) + ); + } private final ObservableList playlist = FXCollections.observableArrayList(); private final ReadOnlyBooleanWrapper canGoBack = new ReadOnlyBooleanWrapper(); diff --git a/sampler/src/main/java/atlantafx/sampler/page/showcase/musicplayer/PlaylistPane.java b/sampler/src/main/java/atlantafx/sampler/page/showcase/musicplayer/PlaylistPane.java index e926bde..275ea8d 100644 --- a/sampler/src/main/java/atlantafx/sampler/page/showcase/musicplayer/PlaylistPane.java +++ b/sampler/src/main/java/atlantafx/sampler/page/showcase/musicplayer/PlaylistPane.java @@ -135,7 +135,7 @@ final class PlaylistPane extends VBox { public Void call() throws InterruptedException { for (File file : files) { Thread.sleep(500); // add artificial delay to demonstrate progress bar - Platform.runLater(() -> model.addFile(new MediaFile(file))); + Platform.runLater(() -> model.addFile(new MediaFile(file.toPath()))); progress++; updateProgress(progress, files.size()); } diff --git a/sampler/src/main/java/atlantafx/sampler/page/showcase/musicplayer/StartScreen.java b/sampler/src/main/java/atlantafx/sampler/page/showcase/musicplayer/StartScreen.java index 9c808db..db78af0 100644 --- a/sampler/src/main/java/atlantafx/sampler/page/showcase/musicplayer/StartScreen.java +++ b/sampler/src/main/java/atlantafx/sampler/page/showcase/musicplayer/StartScreen.java @@ -86,7 +86,7 @@ final class StartScreen extends BorderPane { } for (File file : files) { - model.addFile(new MediaFile(file)); + model.addFile(new MediaFile(file.toPath())); } } @@ -108,7 +108,7 @@ final class StartScreen extends BorderPane { if (!p.toAbsolutePath().toString().endsWith(s)) { continue; } - model.addFile(new MediaFile(p.toFile())); + model.addFile(new MediaFile(p)); } }); } catch (Exception e) { diff --git a/sampler/src/main/java/module-info.java b/sampler/src/main/java/module-info.java index 6b8f608..a670ec5 100755 --- a/sampler/src/main/java/module-info.java +++ b/sampler/src/main/java/module-info.java @@ -10,6 +10,7 @@ module atlantafx.sampler { requires javafx.media; requires javafx.web; requires javafx.fxml; + requires jdk.zipfs; requires org.kordamp.ikonli.core; requires org.kordamp.ikonli.javafx; @@ -39,6 +40,7 @@ module atlantafx.sampler { opens atlantafx.sampler.assets.styles; opens atlantafx.sampler.images; opens atlantafx.sampler.images.modena; + opens atlantafx.sampler.media; opens atlantafx.sampler.page.general; opens atlantafx.sampler.page.showcase; }