我正在尝试使用 JLayer 播放 mp3。该文件在我的 jar 里,但我不知道如何让它播放。我如何为我的应用程序提供与我的类位于同一包中的 mp3 文件的路径? 这是我的代码:
File file = new File("audio.mp3");
System.out.println("located media at "+file.getAbsolutePath());
AdvancedPlayer player = null;
try {
player = new AdvancedPlayer(new FileInputStream(file),
FactoryRegistry.systemRegistry().createAudioDevice());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JavaLayerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Starting the music... ");
try {
player.play();
} catch (JavaLayerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
最佳答案
您不应将File
用于源文件夹中的资源。
尝试
InputStream is = getClass().getResourceAsStream("/(包名)/audio.mp3");
关于java - 使用JLayer播放mp3资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26946826/