简体   繁体   English

Python:在后台播放音乐?

[英]Python: Playing a music in the background?

I am currently making a game in Python. 我目前正在用Python做游戏。 I am not using PyGame, just the console (non-GUI) When you start the game, you will get the logo to the game, and a lot of information about the "journey" you just started. 我使用的不是PyGame,而是控制台(非GUI),当您启动游戏时,会得到游戏的徽标以及有关您刚刚开始的“旅程”的许多信息。 There is a lot of text, so while the text is scrolling, I want to have a song played in the background. 文本很多,所以在滚动文本时,我想在后台播放一首歌。

I start the music with the following code: 我用以下代码开始音乐:

def new_game():
    import winsound
    winsound.PlaySound("intro.wav", winsound.SND_ALIAS)
    LVL1_INTRO()

The only problem is: it won't continue to LVL1_INTRO() until the music have stopped playing. 唯一的问题是:在音乐停止播放之前,它不会继续LVL1_INTRO()。 It's a problem, as the music is approximately 1-2 minutes long. 这是个问题,因为音乐大约需要1-2分钟。

Is there any way to fix this? 有没有什么办法解决这一问题? After the music have started, it will continue with LVL1_INTRO() 音乐开始后,它将继续显示LVL1_INTRO()

If it is possible, I would be happy if there is a code for stopping the music as well, so I don't need to start cutting the music, and make it exactly the same lenght as the intro. 如果可能的话,如果也有用于停止音乐的代码,我会很高兴,因此我不需要开始剪切音乐,并且使其长度与简介完全相同。

Thank you very much! 非常感谢你!

According to the documentation you use the SND_ASYNC flag. 根据文档,您使用SND_ASYNC标志。

winsound.SND_ASYNC 
Return immediately, allowing sounds to play asynchronously.

To stop playing, call PlaySound with a NONE argument. 要停止播放,请使用NONE参数调用PlaySound

winsound.PlaySound(None, winsound.SND_ASYNC)

I don't have experience with this module, but it looks as though you can play sounds asynchronously. 我没有这个模块的经验,但是看起来您可以异步播放声音。 See http://docs.python.org/2/library/winsound.html and look at SND_ASYNC. 请参阅http://docs.python.org/2/library/winsound.html并查看SND_ASYNC。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM