Python Forum
Queue in Pygame - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Queue in Pygame (/thread-23589.html)



Queue in Pygame - constantin01 - Jan-07-2020

Hi

I work on audio player, I use Tkinter for GUI and Pygame for media.

I would like to make a playlist of songs and to play them with pygame.mixer.music

There is a function "queue()", but it doesn't work, actually. In fact, it can take only one song in queue, no more.

So, what I can do? I tried to implement it throut loop:

	running = True;

			while running:

				if (self.mixer.get_busy() == False and files != False):
					self.mixer.load ( self.playlist[files.pop()] )
					self.mixer.play(1)
					
					if (files == False):
						running = False;
						break;
but the problem is that user can't interact with GUI until the loop breaks. It means that user is not able to pause music or stop it.

How I can solve this problem with Pygame? Or it is better to use other library ?

P.S. I browsed GitHub, it turned out that people make audio players with pygame, but it doesn't support playing a playlist of songs.


RE: Queue in Pygame - metulburr - Jan-07-2020

I would not mix tkinter and pygame in one app. Tkinter has a built in loop while pygame you have to build your loop. The result is the issue you are having along with others. I would just use pygame alone. However you have to build your UI. But there are pre-existing buttons UI modules for pygame. I would also just build my own queue system in pygame.

Here is an example of a game i created a while ago. It creates a playlist of whatever is in the directory. But you can make it limited to a whatever playlist you want. Its just an example.

The Music class:
https://github.com/metulburr/pong/blob/master/data/tools.py#L43

The music object:
https://github.com/metulburr/pong/blob/master/data/tools.py#L73

switching to next song when ends
https://github.com/metulburr/pong/blob/master/data/states/classic.py#L55

Note: there is no accommodation for looping since i didnt figure anyone would play pong that long, but that can be easily added by repeating the list

As for pygame buttons you can use a pre-existing button module as state before. And some fully fledged GUI toolkits have file browsers if i remember. Or you can make your own.