2
private static final String MEDIA_URL =     
    "file:///d:/dt_32bars_120tribal.wav";
    //"file:///d:/Linkin Park - Numb.mp3"; 
 Media media = new Media(MEDIA_URL);
 MediaPlayer mediaPlayer = new MediaPlayer(media);    

When I use the file:///d:/dt_32bars_120tribal.wav; the program can work. But when I use the second one: file:///d:/Linkin Park - Numb.mp3; it cannot work.

After I rename the MP3 as LinkinPark-Numb.mp3(remove all space), then I use file:///d:/LinkinPark-Numb.mp3; it can work now.

Does this mean that the Media() cannot allow space in the file name? but as we all know that sometimes the name of mp3 files contain several space. How can I deal with it?

Johannes Kuhn
  • 14,778
  • 4
  • 49
  • 73
jiadong
  • 135
  • 7

1 Answers1

2

You could use File.toURI() to generate a properly escaped URI string:

new java.io.File("abc def.mp3").toURI().toString();
;; => "file:/.../abc%20def.mp3"

See the Media(String) documentation:

The supplied URI must conform to RFC-2396 as required by java.net.URI.

Spaces, e.g., have to be escaped as %20.

xsc
  • 5,983
  • 23
  • 30
  • 1
    [Percent-encoding](http://en.wikipedia.org/wiki/Percent-encoding) - just a way to represent "special" characters (like whitespace) in certain contexts, most prominently URIs/URLs. – xsc Nov 01 '14 at 20:02
  • thank you! one more question, what is the difference between URI and URL. I suppose that i know what is URL – jiadong Nov 03 '14 at 07:53
  • I don't think I can come up with an explanation as good as [this other SO answer](http://stackoverflow.com/a/1984225/1916789), so there you go. :) – xsc Nov 03 '14 at 08:30
  • xsc, thank you very much. although i am not quite clear of all the concepts mentioned there, at least i have some idea of that. – jiadong Nov 04 '14 at 15:29