Search code examples
vb.netmedia-playertrackingplaylistbass

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


so I'm developing a music player based on Un4seen Bass audio library , It does everything correctly except for playlist management which I'm having difficulties to fix it , my playlist system works as following , I have a class called Playlist it contains all the functions for playlist management (adding , removing , tracking(here's the problem) ...etc), It contains a listbox as a playlist , and a timer responsible for tracking Zero-Based index of currently playing file on the Player Class on the Main Form , If i activate the no duplicates mode on the Playlist class it tracks the current playing file just fine and i can get the index correctly , but if i deactivate the no duplicates mode and assume i have (Let's call it Song1 for now) Song1 as the first file on the playlist and i have it also as the Tenth file on the playlist also. The playlist will feed the Player with filenames to start and since the IndexOf function of the playlist get's the first match on the list when it gets to the tenth file which is Song1 it will return to the first file on the list and start looping only those 9 songs , i tried using ` LastIndexOf but still not working correctly , i though of using Unique file names but i get file doesn't exist on IO.File.Exists , Can you guide me ? Here is my Code :

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 : The Playlistalt is a List(of string) because Listbox doesn't have LastIndexOf methode
Also all of the solutions i found are using Windows Media Player , which i used but it's not enough for me
Thank you ^^

Edit : The code for song adding sub

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```

Solution

  • It seems like i found a question (derived from @Idle_Mind comment) ,The code for Next/Back is below

     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