如何跟踪媒体播放器播放列表上的文件名并获取当前播放文件索引

How to track file names on a media player playlist and get current playing file index

所以我正在开发一个基于 Un4seen Bass 音频库的音乐播放器,除了播放列表管理我很难修复它,它可以正确地做所有事情,我的播放列表系统工作如下,我有一个 class called Playlist 它包含播放列表管理的所有功能(添加,删除,跟踪(问题所在)...等),它包含一个列表框作为播放列表,以及一个负责跟踪当前的零基索引的计时器在主窗体的播放器 Class 上播放文件,如果我在播放列表 class 上激活无重复模式,它会很好地跟踪当前播放的文件并且我可以正确获取索引,但如果我停用无重复模式并假设我有(我们现在称它为 Song1)Song1 作为播放列表中的第一个文件,我也将它作为播放列表中的第十个文件。播放列表将为播放器提供文件名以开始播放,因为播放列表的 IndexOf 函数是列表中的第一个匹配项,当它到达第十个文件即 Song1 时,它将 return 到第一个文件在列表上并开始仅循环播放那 9 首歌曲,我尝试使用 `LastIndexOf 但仍然无法正常工作,虽然我使用了唯一文件名但我得到文件在 IO.File.Exists 上不存在,你能引导我 ? 这是我的代码:

Private Sub IndexTimer_Tick(sender As Object, e As EventArgs) Handles IndexTimer.Tick
        Try
            If My.Forms.Form1.Visible = True Then
                If My.Settings.Playlist_Tracking = My.Forms.Settings.PlaylistTracking.FirstIndex Then
                    Index = Playlist.Items.IndexOf(My.Forms.Form1.P1.sourceURL)
                ElseIf My.Settings.Playlist_Tracking = My.Forms.Settings.PlaylistTracking.LastIndex Then
                    Index = Playlistalt.LastIndexOf(My.Forms.Form1.P1.sourceURL)
                End If
            End If
        Catch ex As Exception
            Console.WriteLine(My.Computer.Clock.LocalTime.ToShortTimeString & ": " & ex.Message)
        End Try
    End Sub

PS : PlaylistaltList(of string) 因为 Listbox 没有 LastIndexOf 方法
此外,我找到的所有解决方案都使用 Windows Media Player ,我使用了它,但对我来说还不够
谢谢^^

编辑:歌曲加子代码

Public Sub Add(song As String)
        If IO.File.Exists(song) Then
            If My.Settings.Playlist_RemoveOnAdd = False AndAlso My.Settings.Playlist_Tracking <> My.Forms.Settings.PlaylistTracking.FirstIndexAdvanced Then
                Playlist.Items.Add(song)
                Playlistalt.Add(song)
            ElseIf My.Settings.Playlist_RemoveOnAdd = False AndAlso My.Settings.Playlist_Tracking = My.Forms.Settings.PlaylistTracking.FirstIndexAdvanced Then
                Playlist.Items.Add(song)
                Playlistalt.Add(song)
                RemoveDuplicates(True)
            ElseIf My.Settings.Playlist_RemoveOnAdd = True AndAlso My.Settings.Playlist_Tracking <> My.Forms.Settings.PlaylistTracking.FirstIndexAdvanced Then
                Playlist.Items.Clear()
                Playlist.Items.Add(song)
                Playlistalt.Clear()
                Playlistalt.Add(song)
            ElseIf My.Settings.Playlist_RemoveOnAdd = True AndAlso My.Settings.Playlist_Tracking = My.Forms.Settings.PlaylistTracking.FirstIndexAdvanced Then
                Playlist.Items.Clear()
                Playlist.Items.Add(song)
                Playlistalt.Clear()
                Playlistalt.Add(song)
                RemoveDuplicates(True)
            End If
            RaiseEvent PlaylistItemsChanged()
        Else
            Console.WriteLine(My.Computer.Clock.LocalTime.ToShortTimeString & ": " & "Please check if your file exists then add it to the playlist")
        End If
    End Sub```

我好像发现了一个问题(来源于@Idle_Mind评论),Next/Back的代码如下

 Public Function Next_s()
        Try
                Index += 1
            RaiseEvent Next_song()
            Return Playlist.Items.Item(Index)
        Catch ex As Exception
            Console.WriteLine(My.Computer.Clock.LocalTime.ToShortTimeString & ": " &
ex.Message)
            Try
                    Index = 0
                RaiseEvent Next_song()
                Return Playlist.Items.Item(0)
            Catch ex2 As Exception
                Console.WriteLine(My.Computer.Clock.LocalTime.ToShortTimeString & ":  " & ex2.Message)
            End Try
        End Try
    End Function
Public Function Prev_s()
        Try
                Index -= 1
            Return Playlist.Items.Item(Index)
            RaiseEvent previous_song()
        Catch ex As Exception
            Console.WriteLine(My.Computer.Clock.LocalTime.ToShortTimeString & ": " & ex.Message)
            Try
                    Index = Playlist.Items.Count - 1
                RaiseEvent previous_song()
                Return Playlist.Items.Item(Index)
            Catch ex2 As Exception
                Console.WriteLine(My.Computer.Clock.LocalTime.ToShortTimeString & ": " & ex2.Message)
            End Try
        End Try
    End Function