Search code examples
pythonlistlinked-list

Simple Music Player Using Linked List


I'm making a music player using a circular linked list and python (pygame). I have a delete feature, but apparently it's always bugging. The condition so it will be bugged is :

  1. We delete the first song and there is another song still in the list
  2. We delete immediately after inserting all of our songs

This music player is using mp3 file from our pc directory

def delete_current_song(self, playlist_box):
        if not self.head:
            return

        current_song = self.get_current_song()

        if self.head.next == self.head:
            self.stop_current_song()
            self.head = None
            self.current = None
        else:
            self.stop_current_song()
            temp = self.head

            while temp.next != self.current:
                temp = temp.next

            temp.next = self.current.next
            self.current = temp.next

        self.master.after(10, self.update_playlist_box, playlist_box)

        self.master.after(20, self.play_next_song)

        if current_song:
            self.master.after(30, self.play_current_song)

Here I put it in circular linked list class:

def delete_song(self):
    self.playlist.delete_current_song(self.playlist_box)
    self.update_song_info_label()

And this one in music player class.

So how can I fix the delete function so it will not bugged?


Solution

  • In delete_current_song, in the else block, you must make sure to update self.head when it happens to be the current song that is being removed. Otherwise self.head will still refer to the deleted song.

    So change this:

                temp.next = self.current.next
                self.current = temp.next
    

    to this:

                temp.next = self.current.next
                if self.head == self.current:
                    self.head = temp.next
                self.current = temp.next