1

MediaPlayer works with most of the files, except when there's some characters in the filename.

MediaException: MEDIA_UNAVAILABLE : T:\Music\Paradis - Hémisph?re.m4a (The system cannot find the file specified)

    public static void play(File song) {
    // Checks if file exists
    if (!song.exists()) {
        System.out.println("Song doesn't exist! " + song.getAbsolutePath());
        return;
    }

    Media media = new Media(song.toURI().toString());
    player = new MediaPlayer(media);

    player.play();

}

And the code calling the function:

FXMediaPlayer.play(new File("T:\\Music\\Paradis - Hémisphère.m4a"));

Some of the characters are: é ê ä

How should i correctly parse the filename or is it something wrong with MediaPlayer?

Tomiscout
  • 46
  • 5
  • 1
    For an URI these characters maybe are invalid. Look for further information in the documentation of [URI-Class](https://docs.oracle.com/javase/8/docs/api/java/net/URI.html) in section _Character categories_. Or you can try to change `toString` to `toASCIIString`. – aw-think Sep 27 '15 at 06:03

1 Answers1

1

Solved! I had to encode filename to UTF-8.

Also had to replace '+' with '%20', more info: https://stackoverflow.com/a/4737967/3791826

Doesn't support filenames with UTF-8 chars, because it gave me URISyntaxException

String filePath = null;
    try {
        //Encoding filename to UTF-8, doesn't support folders with UTF-8 characters
        filePath = song.getParentFile().toURI().toString()
                + URLEncoder.encode(song.getName(), "UTF-8").replace("+", "%20");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
Media media = new Media(filePath);
Community
  • 1
  • 1
Tomiscout
  • 46
  • 5