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?
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);