< prev index next >

apps/samples/Ensemble8/src/samples/java/ensemble/samples/media/audioclip/AudioClipApp.java

Print this page
rev 10377 : 8177428: [test] Ensemble cannot play AudioClip files when running in Java 8 and hosted over http
Reviewed-by: kcr

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2008, 2016, Oracle and/or its affiliates.
+ * Copyright (c) 2008, 2017, Oracle and/or its affiliates.
  * All rights reserved. Use is subject to license terms.
  *
  * This file is available and licensed under the following license:
  *
  * Redistribution and use in source and binary forms, with or without

@@ -29,12 +29,15 @@
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 package ensemble.samples.media.audioclip;
 
+import java.io.File;
+import java.net.HttpURLConnection;
+import java.net.URI;
+import java.net.URL;
 import javafx.application.Application;
-import javafx.event.EventHandler;
 import javafx.scene.Group;
 import javafx.scene.Parent;
 import javafx.scene.Scene;
 import javafx.scene.effect.Light;
 import javafx.scene.effect.Lighting;

@@ -100,28 +103,67 @@
         };
         root.getChildren().add(content);
         return root;
     }
 
-    private static final String[] urls = {
-        "/ensemble/samples/shared-resources/Note1.wav",
-        "/ensemble/samples/shared-resources/Note2.wav",
-        "/ensemble/samples/shared-resources/Note3.wav",
-        "/ensemble/samples/shared-resources/Note4.wav",
-        "/ensemble/samples/shared-resources/Note5.wav",
-        "/ensemble/samples/shared-resources/Note6.wav",
-        "/ensemble/samples/shared-resources/Note7.wav",
-        "/ensemble/samples/shared-resources/Note8.wav"
-    };
+    /*
+     * See JDK-8177428 for an explanation of why this is here.
+     */
+    private static AudioClip getNoteClip(String name) {
+        // First look for the clips in a directory next to our jar file
+        try {
+            // Get a URI to this class file
+            URI baseURI = AudioClipApp.class.getResource("AudioClipApp.class").toURI();
+            
+            // If we have a jar URL, get the embedded http or file URL
+            // and trim off the internal jar path, this will leave us
+            // with a URL to the jar file
+            if (baseURI.getScheme().equals("jar")) {
+                String basePath = baseURI.getSchemeSpecificPart();
+                if (basePath.contains("!/")) {
+                    basePath = basePath.substring(0, basePath.indexOf("!/"));
+                }
+                baseURI = new URI(basePath);
+            }
+            
+            URL noteURL = baseURI.resolve("resources/"+name).toURL();
+
+            // check if the resource exists, then try to load it
+            if (noteURL.getProtocol().equals("http")) {
+                HttpURLConnection urlCon = (HttpURLConnection)noteURL.openConnection();
+                urlCon.setRequestMethod("HEAD");
+                urlCon.connect();
+                if (urlCon.getResponseCode() != HttpURLConnection.HTTP_OK) {
+                    noteURL = null;
+                }
+                urlCon.disconnect();
+            } else if (noteURL.getProtocol().equals("file")) {
+                File f = new File(noteURL.getPath());
+                if (!f.exists() || !f.isFile()) {
+                    noteURL = null;
+                }
+            } else {
+                // unsupported protocol
+                noteURL = null;
+            }
+            if (noteURL != null) {
+                return new AudioClip(noteURL.toExternalForm());
+            }
+        } catch (Exception e) {} // fail gracefully
+
+        // Fall back on the embedded clips
+        return new AudioClip(
+                AudioClipApp.class.getResource("/ensemble/samples/shared-resources/"+name).toExternalForm());
+    }
 
     public static Rectangle createKey(Color color, double x,
                                       double width, int note) {
 
         double height = 100 - ((note - 1) * 5);
         // create a audio clip that this key will play
-        final AudioClip barNote = new AudioClip(
-                AudioClipApp.class.getResource(urls[note - 1]).toExternalForm());
+        final AudioClip barNote = getNoteClip("Note"+note+".wav");
+
         // create the rectangle that draws the key
         Rectangle rectangle = new Rectangle(x, -(height / 2), width, height);
         rectangle.setFill(color);
         Lighting lighting = new Lighting(new Light.Point(-20, -20, 100, Color.WHITE));
         lighting.setSurfaceScale(1);
< prev index next >