2011-04-22 62 views
1

好吧,我已經嘗試使用ID3標籤來獲取持續時間,我也嘗試使用JMF媒體player.getDuration()。在Java中查找MP3文件的持續時間

player.getDuration().getSeconds() 

該文件是VBR。 JMF內部是否有輕量級的庫或者可以用來解決這個問題的東西?

謝謝。

+0

你不能從流中獲取AudioFormat對象,然後採樣率和文件大小的組合應該給你的持續時間? – onnoweb 2011-04-22 20:00:25

回答

4

我使用JAudioTagger來實現這一點。以下代碼將爲您帶來MP3曲目的持續時間。

int duration = 0; 

try { 
    AudioFile audioFile = AudioFileIO.read(new File("file.mp3")); 
    duration = audioFile.getAudioHeader().getTrackLength(); 

} catch (Exception e) { 
    e.printStackTrace(); 

} 

您也可以施放audioFile.getAudioHeader()來MP3AudioHeader和使用方法getPreciseTrackLength(),以獲得更精確的時間。然而,這(我相信)只適用於MP3文件,並沒有其他格式(如WAV文件)。

+0

P.S! AudioHeader(由audioFile.getAudioHeader()返回)還會爲您提供MP3文件的比特率以及許多其他屬性。 – sbrattla 2011-04-22 20:21:04

0

我用這個lib和驗證碼:

File f = new File("path/mp3"); 
    MediaLocator ml = null; 
    Player p = null; 
    try { 
     ml = new MediaLocator(f.toURL()); 

     p = Manager.createPlayer(ml); 
     p.start(); 
     while (true) { 
      Thread.sleep(1000); 
      System.out.println("Media Time :: "+p.getMediaTime().getSeconds()); 
      System.out.println("Duration :: "+p.getDuration().getSeconds()); 
      if(p.getMediaTime().getSeconds() == p.getDuration().getSeconds()) 
       break; 
     } 
     p.stop(); 
     p.deallocate(); 
     p.close(); 
    } catch (NoPlayerException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 

好運。

+0

當我點擊鏈接到圖書館時,它給出了圖書館的描述,但是當我點擊進行下載時,所描述的圖書館在下載頁面上實際上不可用。這個庫有沒有可用的maven工件? – Jules 2017-10-30 06:00:17

相關問題