Calculating Total Audio Duration from listview with WMPLIB

jdkoops

Member
Joined
Sep 5, 2008
Messages
17
Programming Experience
Beginner
Hi,

I have a form that loads mp3 file names into a listview with another column for their duration.

code:
Dim fname As String
fname = System.IO.Path.GetFileName(OpenFileDialog2.FileName)

Dim Duration As String = 0
Dim w As New WMPLib.WindowsMediaPlayer
Dim m As WMPLib.IWMPMedia = w.newMedia(fname)
If m IsNot Nothing Then
Duration = m.durationString

End If
w.close()

ListView1.Items.Add(fname).SubItems.Add(Duration)


I would like to have a label or textbox that has total time for all files in the listview and something that will re-calculate
if i delete a file from the listview.

I know the code I'm using is converting to string which is probably the problem; but I'm not sure. Looked
everywhere i could on WMPlib and couldn't find an answer/figure it out.

Any help is appreciated.

Thanks,
J
 
I found this code on the net and added the loop for playlist creation so
making some progress with getting total duration from mp3 listbox... into a msgbox.
Now trying to figure out how to get the duration displayed as hours/mins/secs.

Any thoughts?

code so far:


If ListBox2.Items.Count = 0 Then
Exit Sub

End If
' Create the Media Player.
Dim plyr As New WMPLib.WindowsMediaPlayer()
With plyr
' Create the PlayList.
Dim plyList As WMPLib.IWMPPlaylist = .newPlaylist("plyList", "")

' Add some media.

Dim item As String
For Each item In ListBox2.Items

Dim med1 As WMPLib.IWMPMedia = .newMedia(item)
plyList.appendItem(med1)

Next

Dim totalTime As Double = 0
For i As Integer = 0 To plyList.count - 1
totalTime += plyList.Item(i).duration
Next i

MessageBox.Show(totalTime / 60, "Duration").ToString()
.close()

End With
 
Last edited:
Hi,

Assuming that the total time that you have currently calculated from the ListView is the total Seconds then you can use the TimeSpan Structure to convert this value to a recognisable hh:mm:ss format. Here is a quick example:-

Dim totalDurationInSeconds As Integer = 12345
Dim myTimeSpan As New TimeSpan(totalDurationInSeconds * TimeSpan.TicksPerSecond)
 
MsgBox(myTimeSpan.Duration.ToString)


If your current value is not in seconds then you will be able to get the same result with a bit of additional maths.

Hope that helps.

Cheers,

Ian
 
Back
Top