Python:在后台播放音乐? [英] Python: Playing a music in the background?

查看:604
本文介绍了Python:在后台播放音乐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在用Python做游戏.我不使用PyGame,仅使用控制台(非GUI) 当您开始游戏时,您将获得游戏的徽标,以及有关您刚刚开始的旅程"的许多信息.文本很多,所以在滚动文本时,我想在后台播放一首歌.

I am currently making a game in 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. 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()

唯一的问题是:在音乐停止播放之前,它不会继续LVL1_INTRO().这是一个问题,因为音乐大约需要1-2分钟.

The only problem is: it won't continue to LVL1_INTRO() until the music have stopped playing. It's a problem, as the music is approximately 1-2 minutes long.

有什么办法可以解决这个问题?音乐开始播放后,它将继续显示LVL1_INTRO()

Is there any way to fix this? After the music have started, it will continue with 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.

非常感谢!

推荐答案

根据

According to the documentation you use the SND_ASYNC flag.

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

要停止播放,请使用NONE参数调用PlaySound.

To stop playing, call PlaySound with a NONE argument.

winsound.PlaySound(None, winsound.SND_ASYNC)

这篇关于Python:在后台播放音乐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆