More efficient way to find media item in WMP media library?
- by RoseOfJericho
Hello, all.
I am messing around with the WMPLib component provided by Windows Media Player 12 (wmp.dll) in VB.NET with .NET Framework 3.5 SP1.
I am trying to retrieve a media item from my media library based on its name (assuming there are no duplicate names). At the moment, I'm grabbing the entire media library, and looping through every media item, and quitting the loop when I've found the correct media item. This works well (except for when a media item with that name cannot be found), but I was hoping there was a more efficient way of doing this.
Here is my code so far:
Public Class WMPTest
Private myWMP As WMPLib.IWMPCore
Private myMediaCollection As WMPLib.IWMPMediaCollection
Private myTrack As WMPLib.IWMPMedia
Private allTracks As WMPLib.IWMPPlaylist
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
myWMP = New WMPLib.WindowsMediaPlayer
myMediaCollection = myWMP.mediaCollection
allTracks = myMediaCollection.getAll
Dim theTrack As WMPLib.IWMPMedia = findTrack("Yellow Submarine")
MessageBox.Show(theTrack.name)
End Sub
Public Function findTrack(ByVal strTrackName As String) As WMPLib.IWMPMedia
For i As Integer = 0 To (allTracks.count - 1)
If allTracks.Item(i).name = strTrackName Then
myTrack = allTracks.Item(i)
Exit For
End If
Next
'myTrack is now the track that we wanted to retrieve
Return myTrack
End Function
End Class
So what I really want is a way to optimize findTrack() to do its thing without looping through the entire media library (which could be huge). Anyone have a clue?