How to create a seekbar in Tkinter for Pygame mixer channels?

Viewed 23

In my code, I use the Pygame mixer channel feature to play multiple parts and have the user control each part's volume. I want to create a seekbar so the user can change the position of the track. How could I change the channel's position?

Example of what the code would look like:

from tkinter import *
from tkinter import ttk
from os import environ
environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide" # Stop pygame's welcome message popping up in console
from pygame import mixer
from mutagen.wave import WAVE
from pandas import read_json

class app(Tk):
    def __init__(self):
        Tk.__init__(self)
        
        self.title("Music Player")
        
        # Play the song
        self.file = "music/stems/" + songFile # Song file being a variable called from a JSON file
        
        self.channel1 = mixer.Channel(0)
        channel1Sound = mixer.Sound(self.file + "/bass.wav")
        self.channel1.play(channel1Sound)

        self.channel2 = mixer.Channel(1)
        channel2Sound = mixer.Sound(self.file + "/drums.wav")
        self.channel2.play(channel2Sound)

        self.channel3 = mixer.Channel(2)
        channel3Sound = mixer.Sound(self.file + "/other.wav")
        self.channel3.play(channel3Sound)

        self.channel4 = mixer.Channel(3)
        channel4Sound = mixer.Sound(self.file + "/piano.wav")
        self.channel4.play(channel4Sound)

        self.channel5 = mixer.Channel(4)
        channel5Sound = mixer.Sound(self.file + "/vocals.wav")
        self.channel5.play(channel5Sound)
        
        # Add the seek bar
        song = WAVE(self.file + "/bass.wav") # Any of the files would work
        songDuration = song.info.length
        
        self.progressBar = ttk.Scale(self, from_ = 0, to = songDuration, orient = HORIZONTAL, length = 520) # How would I get the scale to move along with the song?
        
        self.progressBar.bind("<ButtonRelease-1>", self.changeSongPosition)
        self.progressBar.set(0)
        self.progressBar.grid(row = 4, columnspan = 5)
    
    def changeSongPosition(self, event):
        channel4Sound = mixer.Sound(self.file + "/piano.wav")
        self.channel4.play(channel4Sound) # How would I get the channel to move to the correct point?
        
if __name__ == "__main__":    
    mixer.pre_init(0, -16, 5, 512)
    mixer.init()
    
    # Run the app
    app = app()
    
    app.mainloop()
        
0 Answers
Related