在 WMP 媒体库中查找媒体项目的更有效方法?

发布于 2024-08-30 07:55:59 字数 1412 浏览 4 评论 0原文

我正在使用 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞取 2024-09-06 07:55:59

只是考虑如何尝试进行更快的搜索。我可能会创建几个后台工作人员,然后在其中一个工作人员中从 0 迭代到 allTracks.count - 1,然后在另一个工作人员中从 allTracks.count - 1 向下迭代到 0。然后,无论哪个先到达, RunWorkerCompleted 事件,您可以取消其他工作线程上的异步。这可能会将搜索时间缩短一半。

您还可以同时在列表的四分之一上运行循环,方法是将 (count -1) 除以 4,得到 25%,然后从 (count-1) 中减去它,得到 75%,然后向上或向下迭代到一半指向另外几个异步工作人员。这实际上可以将您的搜索时间减少到先前等待时间的 25%。

我看到你正在创建一个类来支持你在对象内的搜索,但我不知道Backgroundworkers是否真的在类内起作用,所以我可能会偏离。

只是一个想法。我以前没有做过这样的事情,所以我可能会让事情变得比需要的更加困难。祝你好运!让我知道你发现了什么。

编辑:如果您使用后台工作人员,我认为您需要在 for 循环中包含此代码:

If BackgroundWorker1.CancellationPending Then
    Exit For
End If

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:

If BackgroundWorker1.CancellationPending Then
    Exit For
End If
路弥 2024-09-06 07:55:59

Simply use the "getByName" method of the IWMPMediaCollection interface ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文