Skip to content

Instantly share code, notes, and snippets.

Last active Jul 14, 2019
What would you like to do?
headless pygame (without a window) playing two sounds at different times.
""" headless pygame (without a window) playing two sounds one after the other.
import os, sys, time
import pygame
import pygame.examples
# a pretend video.
os.environ["SDL_VIDEODRIVER"] = "dummy"
def pygame_example_data(fname):
return os.path.join(os.path.split(pygame.examples.__file__)[0], "data", fname)
file1 = pygame_example_data("boom.wav")
file2 = pygame_example_data("car_door.wav")
when_sound1 = 0.5
when_sound2 = 2.5
clock = pygame.time.Clock()
screen = pygame.display.set_mode((1,1))
sound1 = pygame.mixer.Sound(file1)
sound2 = pygame.mixer.Sound(file2)
second_played = 0
FIRST = pygame.event.custom_type()
SECOND = pygame.event.custom_type()
# play things once...
pygame.time.set_timer(FIRST, int(when_sound1 * 1000), 1)
pygame.time.set_timer(SECOND, int(when_sound2 * 1000), 1)
going = True
while going:
for event in pygame.event.get():
if event.type == FIRST:
if event.type == SECOND:
second_played =
if second_played and not second_played.get_busy():
going = False
# without this there is "Fatal Python error: PyEval_SaveThread: NULL tstate\nAbort trap: 6"
# without this and second_played.get_busy() above there is a segfault.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment