MediaPlayer适用于大多数文件,但文件名中包含某些字符时除外。
MediaException:MEDIA_UNAVAILABLE:T:\ Music \ Paradis-Hémisph?re.m4a(系统找不到指定的文件)
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();
}
和调用该函数的代码:
FXMediaPlayer.play(new File("T:\\Music\\Paradis - Hémisphère.m4a"));
其中一些字符是:éêä
我应该如何正确解析文件名,或者MediaPlayer有问题?
解决了!我必须将文件名编码为UTF-8。
还必须将'+'替换为'%20',更多信息:https : //stackoverflow.com/a/4737967/3791826
不支持UTF-8字符的文件名,因为它给了我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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句