在 WMP 媒体库中查找媒体项目的更有效方法?
我正在使用 .NET Framework 3.5 SP1 的 VB.NET 中的 Windows Media Player 12 (wmp.dll) 提供的 WMPLib 组件。
我正在尝试根据媒体库的名称检索媒体项目(假设没有重复的名称)。目前,我正在获取整个媒体库,并循环遍历每个媒体项目,并在找到正确的媒体项目时退出循环。这很有效(除非找不到具有该名称的媒体项目),但我希望有一种更有效的方法来做到这一点。
到目前为止,这是我的代码:
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
所以我真正想要的是一种优化 findTrack() 的方法,使其能够在不循环整个媒体库(可能很大)的情况下完成其工作。有人知道吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只是考虑如何尝试进行更快的搜索。我可能会创建几个后台工作人员,然后在其中一个工作人员中从 0 迭代到 allTracks.count - 1,然后在另一个工作人员中从 allTracks.count - 1 向下迭代到 0。然后,无论哪个先到达, RunWorkerCompleted 事件,您可以取消其他工作线程上的异步。这可能会将搜索时间缩短一半。
您还可以同时在列表的四分之一上运行循环,方法是将 (count -1) 除以 4,得到 25%,然后从 (count-1) 中减去它,得到 75%,然后向上或向下迭代到一半指向另外几个异步工作人员。这实际上可以将您的搜索时间减少到先前等待时间的 25%。
我看到你正在创建一个类来支持你在对象内的搜索,但我不知道Backgroundworkers是否真的在类内起作用,所以我可能会偏离。
只是一个想法。我以前没有做过这样的事情,所以我可能会让事情变得比需要的更加困难。祝你好运!让我知道你发现了什么。
编辑:如果您使用后台工作人员,我认为您需要在 for 循环中包含此代码:
Just thinking about how I might try to conduct a faster search. I might create a couple Background Workers and then iterate through from 0 to allTracks.count - 1 in one of the workers and then in the other worker, iterate down from allTracks.count - 1 to 0. Then, whichever reaches it first, on the RunWorkerCompleted event, you can cancel async on the other worker. That could potentially cut the search time in half.
You could also run loops on a quarter of the list simultaneously by dividing (count -1) by 4 for 25%, and then you can subtract it from (count-1) for 75%, and then iterate up or down to the halfway point in another couple asynchronous workers. That could feasibly cut down your search time to 25% of the previous wait.
I see you're making a class to support your searching within the object and I don't know if Backgroundworkers actually function inside classes, so I could be way off.
Just an idea. I haven't done anything like this before, so I might be making it way harder than it needs to be. Good luck! Let me know what you find out.
Edit: If you go with background worker, I think you'll need to include this code within the for loop:
只需使用“getByName” IWMPMediaCollection 的方法 界面;)
Simply use the "getByName" method of the IWMPMediaCollection interface ;)