0

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```
Anes Hamdani
  • 50
  • 1
  • 7
  • How do the music files get added to the playlist? Can that code be modified? – preciousbetine Jun 11 '20 at 12:39
  • I added the code to the question , see the edit , and that FirstIndexAdvanced is not a big deal , its just an attempt i tried to fix this issue – Anes Hamdani Jun 11 '20 at 13:35
  • Without seeing more code, my first question would be...why are you SEARCHING for the "next" song with IndexOf() and LastIndexOf()? If you have an "index" then just increase it by 1 (one) and get the song at that position (checking for upper bound of course). – Idle_Mind Jun 11 '20 at 13:55
  • @Idle_Mind if you're right that means change 50% of my application code T-T , but i did take the risk and tried your solution , surprisingly i found my self already made the 25% and gave up (i don't know why) so i completed the rest of stuff , I'll be doing some tests and see if this fix up the problem i'll make it as an answer – Anes Hamdani Jun 11 '20 at 14:55
  • Why not create a song class? When adding a song, you create a new instance of the class then maybe... the class has a property `index` that you can increase whenever you add a new song. To add the song to the listbox, just do `Playlist.Items.Add(instanceOtSongClass.Name)`. Also keep a variable `currentSong` somewhere that tracks the current instance of the song playing. To get the next song, simple fetch `Playlist.Items(currentSong.Index + 1)`. Good luck! – preciousbetine Jun 12 '20 at 08:20
  • @preciousbetine I already found a workaround (maybe fix) for my issue , I gave up using IndexOf because It won't work when there is two file names with same name , So i just added Index += 1 , And Index -= 1 , on both NextSong , PrevSong Subs , and it seems to work correctly , i wouldn't say it doesnt have any Bugs , but it works for now – Anes Hamdani Jun 12 '20 at 14:23

1 Answers1

0

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
Anes Hamdani
  • 50
  • 1
  • 7