Java将inputStream转换为URL

san*_*ixx 5 java swing jfxtras

如何将inputStream转换为URL?这是我的代码:

InputStream song1 = this.getClass().getClassLoader().getResourceAsStream("/songs/BrokenAngel.mp3");
URL song1Url = song1.toUrl(); //<- pseudo code
MediaLocator ml = new MediaLocator(song1Url);
Player p;
try {
    p = Manager.createPlayer(ml);
    p.start();
} catch (NoPlayerException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

Rom*_*ner 2

请注意,此方法要求 mp3 位于应用程序名为“歌曲”的子目录中。您还可以使用该/songs/BrokenAngel.mp3部分的相对路径(../../ 或类似的东西。但它以您的应用程序目录为基础!

    File appDir = new File(System.getProperty("user.dir"));
    URI uri = new URI(appDir.toURI()+"/songs/BrokenAngel.mp3");
    // just to check if the file exists
    File file = new File(uri);
    System.out.println(file.exists())
    URL song1Url = uri.toURL();
Run Code Online (Sandbox Code Playgroud)