1

I'm trying to upload an MP3 file to a site and finding the duration of the audio at the same time. I'm quite new to ASP and VB.NET, but I've managed to get the file to upload to the server using a file upload tool.

However I can't seem to figure out how to read the duration of the audio file?

I'm not particularly looking for a complete solution (although that would be nice), but if anyone could point me in the right direction I'd be very appreciative.

If you require any more information let me know and I'll add it to the question.

ACynicalGeek
  • 79
  • 3
  • 8
  • Is this information available in the id3 tag> Not sure... but if it is, you can use this approach: http://stackoverflow.com/questions/1750464/how-to-read-and-write-id3-tags-to-an-mp3-in-c or http://www.codeproject.com/Articles/13328/Reading-ID3-tags-from-a-WMA-file-MP3-as-well – Brian Mains May 03 '13 at 17:14
  • similar question as this [http://stackoverflow.com/questions/4972194/determine-the-length-in-milliseconds-of-an-mp3-file-in-vb-net] – ajakblackgoat May 03 '13 at 17:39

3 Answers3

1

I've done this in a recent project using naudio (download using package manager). I've converted the code from c# quickly without testing so double check for errors.

    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
Jay Zelos
  • 1,409
  • 1
  • 9
  • 10
0

I'm not convinced the "* 2.0" in the previous answer is appropriate. I tried this, and resulting duration was two times the actual, for a stereo mp3 sound sample. I suggest the following code, successfully tested in MSVS Community 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
grego211
  • 11
  • 1
0

hii add this code to trackbar timer dont forgot to add laber to player

 Label4.Text = AxWindowsMediaPlayer1.Ctlcontrols.currentItem.durationString
betrice mpalanzi
  • 1,436
  • 12
  • 16