2015-09-26 31 views
1

MediaPlayer適用於大多數文件,除非文件名中包含某些字符。MediaPlayer不使用文件名中的某些字符

MediaException:MEDIA_UNAVAILABLE:T:\音樂\天堂 - Hémisphre.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")); 

部分字符有:éêä

我該如何糾正ly解析文件名還是MediaPlayer有問題?

+1

對於一個URI這些字符可能無效。請在_Character categories_部分的[URI-Class](https://docs.oracle.com/javase/8/docs/api/java/net/URI.html)文檔中查找更多信息。或者您可以嘗試將'toString'更改爲'toASCIIString'。 – NwDev

回答

1

解決!我不得不將文件名編碼爲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); 
相關問題