如何在python中播放声音(Windows)

时间:2018-12-15 15:16:46

标签: python windows

我当时用python做游戏,想添加声音,所以我这样做了。想知道是否有一种播放声音但不暂停其余代码的方法吗? (正在使用winsound)

2 个答案:

答案 0 :(得分:0)

使用可以使用winsound.SND_ASYNC来播放而无需停止代码。
玩:
    import winsound
    winsound.PlaySound("filename", winsound.SND_ASYNC | winsound.SND_ALIAS )
要停止:
    winsound.PlaySound(None, winsound.SND_ASYNC)
Original comment

答案 1 :(得分:0)

使用winsound.SND_ASYNC异步播放它们

import winsound
winsound.PlaySound("filename", winsound.SND_ASYNC | winsound.SND_ALIAS )
#To stop playing

winsound.PlaySound(None, winsound.SND_ASYNC)
#In mac or other platforms: You can try this Pygame/SDL

pygame.mixer.init()
pygame.mixer.music.load("file.mp3")
pygame.mixer.music.play()