2

I am trying out Python's pygame module to test its ability to play sound from a file. However, I am building a larger program where I can play this sound in a separate thread while the regular program continues to execute. Below I have posted a simple example of what I plan to do. Here is my code:

import pygame
import time
from threading import Thread


def play_alarm2():
    pygame.mixer.init()
    sound = pygame.mixer.Sound("/home/souvik/Downloads/alarm_beep.wav")
    sound.play()
    time.sleep(4)

alert = False
count = 0
while count <= 10:
    print("Alive!")
    if count == 5:
        print("Dead!")
        t = Thread(target=play_alarm2)
        t.daemon = True
        t.start()

    count += 1

Here as soon as the count hits 5, it should make a sound. But unfortunately, I don't hear any sound which makes me thinking if the thread actually starts. What could I be doing wrong?

skrx
  • 19,980
  • 5
  • 34
  • 48
Souvik Ray
  • 2,899
  • 5
  • 38
  • 70
  • 1
    It works correctly for me (with one of my wav files). Try to remove the threading code and just play the sound, and also check out if you can play the wav in other applications. --- You usually don't need multithreading in pygame programs, just load the sound and play it. If you need some kind of timer, take a look at [these answers](https://stackoverflow.com/q/30720665/6220679) (pygame.time.get_ticks, pygame.time.set_timer, or the delta time solution). – skrx Feb 15 '18 at 14:07

0 Answers0