无法启动播放声音的线程

时间:2018-02-15 12:57:46

标签: python multithreading python-3.x pygame

我正在尝试使用Python的pygame模块来测试它从文件中播放声音的能力。但是,我正在构建一个更大的程序,我可以在常规程序继续执行时在单独的线程中播放此声音。下面我发布了一个我计划做的简单示例。这是我的代码:

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

一旦计数达到5,就应该发出声音。但不幸的是,我没有听到任何让我思考线程是否实际启动的声音。我能做错什么?

0 个答案:

没有答案