2014-11-15 93 views
0

我正在嘗試使用JLayer播放mp3。該文件在我的jar中,但我無法弄清楚如何使它發揮。我將如何給我的應用程序的MP3文件的路徑與我的類在相同的包? 這裏是我的代碼:使用JLayer播放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(); 
} 

回答

0

你爲什麼不嘗試絕對路徑播放歌曲。使用此代碼以供參考:

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

請注意在路徑中使用雙「\」。