Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8118531

AudioClip: does not play sound when path/URL contains space character

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: fx2.0
    • Fix Version/s: fx2.0
    • Component/s: javafx
    • Environment:

      Windows 7 64bit, Java 1.7.0 64bit, JavaFX b38 64bit

      Description

      When playing a file from the disk, AudioClip fails to play the sound when the path to the file contains a space character (the URL contains the %20 escaped sequence).
      There is no sound being played and no error is outputed.

      Sound files which do not contain spaces in their names are played Ok.

      I haven't tested if this happens the same when accessing an audio file within an archive or from the web.

      /*
       * To change this template, choose Tools | Templates
       * and open the template in the editor.
       */
      package test;

      import java.io.File;
      import java.net.URL;
      import java.util.ArrayList;
      import java.util.List;
      import javafx.application.Application;
      import javafx.builders.FlowPaneBuilder;
      import javafx.collections.FXCollections;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.layout.FlowPane;
      import javafx.scene.media.AudioClip;
      import javafx.stage.Stage;

      /**
       *
       * @author fabriceb
       */
      public class Main extends Application {

          /**
           * @param args the command line arguments
           */
          public static void main(String[] args) {
              Application.launch(Main.class, args);
          }

          @Override
          public void start(Stage primaryStage) {
              String[] files = {
                  "C:\\Windows\\Media\\ding.wav", // Sound file from the disk.
                  "file:/c:/Windows/Media/chord.wav", // Sound file from the disk (URL-style).
                  "C:\\Windows\\Media\\Afternoon\\Windows Balloon.wav",
                  "file:/C:/Windows/Media/Afternoon/Windows%20Balloon.wav",
              };
              List<Button> buttonList = new ArrayList<>(files.length);
              for (String file : files) {
                  final String f = file;
                  Button button = new Button(file);
                  button.setOnAction(new EventHandler<ActionEvent>() {

                      public void handle(ActionEvent event) {
                          play(f);
                      }
                  });
                  buttonList.add(button);
              }
              FlowPane buttonPane = FlowPaneBuilder.create().id("buttonPane").hgap(5).vgap(5).children(FXCollections.observableArrayList(buttonList)).build();
              Scene scene = new Scene(buttonPane);
              primaryStage.setTitle("AudioClip + resources access test");
              primaryStage.setWidth(500);
              primaryStage.setHeight(500);
              primaryStage.setScene(scene);
              primaryStage.setVisible(true);
          }

          private void play(String resourcePath) {
              try {
                  URL url = null;
                  if (resourcePath.startsWith("http:") || resourcePath.startsWith("file:")) {
                      url = new URL(resourcePath);
                  } else {
                      ClassLoader loader = Main.class.getClassLoader();
                      url = loader.getResource(resourcePath);
                  }
                  if (url == null) {
                      File file = new File(resourcePath);
                      url = file.toURI().toURL();
                  }
                  AudioClip audioClip = new AudioClip(url.toExternalForm());
                  audioClip.play();
              } catch (Throwable t) {
                  t.printStackTrace();
              }
          }
      }

        Attachments

          Activity

            People

            • Assignee:
              almatvee Alexander Matveev
              Reporter:
              fbouyajfx Fabrice Bouyé (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported: