2013-05-03 19 views
1

我試圖將MP3文件上傳到網站並同時查找音頻的持續時間。我對ASP和VB.NET很陌生,但我設法使用文件上傳工具將文件上傳到服務器。VB.NET/ASP.NET - 獲取上傳MP3文件的時間

但是我似乎無法弄清楚如何讀取音頻文件的持續時間?

我並不特別尋找一個完整的解決方案(雖然那會很好),但如果任何人都可以指出我在正確的方向,我會非常感激。

如果您需要更多信息,請讓我知道,我會將其添加到問題中。

+0

可用此信息在id3標籤>不知道...但如果是這樣,你可以使用這種方法:http://stackoverflow.com/questions/1750464/how-to-read-and-write-id3-tags-to-an -mp3-in-c或http://www.codeproject.com/Articles/13328/Reading-ID3-tags-from-a-WMA-file-MP3-as-wel l – 2013-05-03 17:14:29

+0

類似的問題,因爲這[http://stackoverflow.com/questions/4972194/determine-the-length-in-milliseconds-of-an-mp3-file-in-vb-net] – ajakblackgoat 2013-05-03 17:39:58

回答

0

我在最近的一個項目中使用naudio(使用包管理器下載)完成了這項工作。我已經從c#中快速轉換了代碼,而無需進行測試,所以仔細檢查錯誤。

private shared function GetMp3Duration(filename as string) as double 
     Dim duration as double = 0.0 
     using (fs as FileStream = File.OpenRead(filename)) 
      Dim frame as Mp3Frame = Mp3Frame.LoadFromStream(fs) 
      while (frame isnot nothing) 
       if (frame.ChannelMode = ChannelMode.Mono) then 
        duration += frame.SampleCount/frame.SampleRate 
       else 
        duration += frame.SampleCount * 2.0/frame.SampleRate 
       end if 
       frame = Mp3Frame.LoadFromStream(fs) 
      End While 
     End Using 
     return duration 
    End Function 
0

我不相信上一個答案中的「* 2.0」是合適的。我試了這個,結果持續時間是實際的兩倍,對於立體聲mp3的聲音樣本。我建議下面的代碼,在MSVS社區2013測試成功:

Imports NAudio.Wave 
'... 
Private Shared Function GetMp3Duration(filename As String) As Double 
    Dim reader As New Mp3FileReader(filename) 
    Dim duration As Double = reader.TotalTime.TotalSeconds 
    reader.Dispose() 
    Return duration 
End Function 
0

HII將此代碼添加到的TrackBar定時器不要忘了關於勞動添加到播放器

Label4.Text = AxWindowsMediaPlayer1.Ctlcontrols.currentItem.durationString