【问题标题】:Use JLayer to play mp3 resource使用JLayer播放mp3资源
【发布时间】:2014-11-15 14:26:44
【问题描述】:

我正在尝试使用 JLayer 播放 mp3。该文件在我的罐子里,但我不知道如何让它播放。如何为我的应用程序提供与我的类在同一个包中的 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();
}

【问题讨论】:

    标签: java mp3 jlayer


    【解决方案1】:

    您不应将File 用于源文件夹中的资源。
    试试
    InputStream is = getClass().getResourceAsStream("/(package name)/audio.mp3");

    【讨论】:

      【解决方案2】:

      你为什么不试试绝对路径来播放这首歌。使用此代码作为参考:

      FileInputStream fis = new FileInputStream("G:\\Songs\\fireinthehole.mp3");
      player = new AdvancedPlayer(fis);
      player.play();
      

      注意在路径中使用双“\”。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-21
        • 2014-08-14
        • 2013-05-28
        • 2013-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多