M
M
Maxim Belozerov2020-10-24 18:57:49
pygame
Maxim Belozerov, 2020-10-24 18:57:49

How to track if a song is currently playing in pygame?

Good afternoon! I work with pygame. I want it to keep track of whether the music (played by pygame) is currently playing. So that it can be used in an if.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan, 2020-10-24
@Python_py

I assume that you need to track the moment when the melody playback is completed.
In this case, it is better to use custom events.
For example.
In the part of the code where the initial initialization takes place:

pygame.mixer.init()
STOPPED_PLAYING = pygame.USEREVENT + 1
pygame.mixer.music.set_endevent(STOPPED_PLAYING)

This is where the STOPPED_PLAYING custom event is initialized and set to the pygame mixer.
Then, in the event loop, you need to add the following:
for event in pygame.event.get():
    if STOPPED_PLAYING == event.type:
        # код для выполнения при окончании воспроизведения музыки
        # например выбор и начало воспроизведения следующей мелодии

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question