2

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!

Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
Mihkel
  • 689
  • 7
  • 34
  • Welcome to Stackoverflow! Here we don't mark our questions as SOLVED. If you have an answer to the question, then post the answer in the answer question. Currently, the community will treat this as an unanswered (unsolved) question, as it doesn't have any answers. – Ted Klein Bergman Jan 30 '19 at 19:19
  • 1
    Oh, I'm sorry. Writing an answer right now! – Mihkel Jan 30 '19 at 19:22

1 Answers1

5

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

Mihkel
  • 689
  • 7
  • 34