Search code examples
if-statementaudiopygamemixer

Check If A PyGame Mixer Channel Is Playing A Sound


I am looking for a way to see if a pygame.mixer.Channel is currently playing a sound. So for example do something only after the sound played in a specific channel has finished. Here is my current code to just play the sound:

import pygame

pygame.mixer.pre_init()
pygame.mixer.init()
pygame.init()

pygame.mixer.Channel(0).play(pygame.mixer.Sound('coolsound.wav'), maxtime=2000)

I was thinking an if statement something like this:

if pygame.mixer.Channel(0) = playing a sound:
    print("playing a sound")
else:
    print("not playing")

Obviously this wouldn't work, just to give you an idea of what I am looking for. Thanks!


Solution

  • I got my answer. Using get_busy() I am able to check wether a channel is playing a sound or not, it returns True or False. Here was my final code:

    if pygame.mixer.Channel(0).get_busy() == True:
        print("playing a sound")
    else:
        print("not playing")
    

    Here is a link to the documentation for more information: https://www.pygame.org/docs/ref/mixer.html#pygame.mixer.get_busy