程序运行时,如果按Enter键如何停止音乐? [英] How to stop music if Enter key is pressed anytime the program is running?

查看:187
本文介绍了程序运行时,如果按Enter键如何停止音乐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的程序执行以下操作:

I want my program do something along the lines of:

此程序正在运行:
    如果按下Enter键,则停止播放当前的音乐文件.

while this program is running:
    if the Enter key is pressed, stop the current music file playing.

这是我的代码:

# https://docs.python.org/2/library/winsound.html

from msvcrt import getch
import winsound

while True:
    key = ord(getch())
    if key == 13:
        winsound.PlaySound(None, winsound.SND_NOWAIT)

winsound.PlaySound("SystemAsterisk", winsound.SND_ALIAS)
winsound.PlaySound("SystemExclamation", winsound.SND_ALIAS)
winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
winsound.PlaySound("SystemHand", winsound.SND_ALIAS)
winsound.PlaySound("SystemQuestion", winsound.SND_ALIAS)

winsound.MessageBeep()

winsound.PlaySound('C:/Users/Admin/My Documents/tone.wav', winsound.SND_FILENAME)

winsound.PlaySound("SystemAsterisk", winsound.SND_ALIAS)


在文档中(请参阅代码第一行中的链接),我不确定天气winsound.SND_NOWAIT是否可以这样使用:winsound.SND_NOWAIT(),或者像我尝试在语句,或者两个语句产生相同的效果.


In the documentation (see link in the first line of my code), I am unsure weather winsound.SND_NOWAIT can be used like this: winsound.SND_NOWAIT(), or like how I tried to use it in my code under the if statement, or if both statements produce the same effect.

据我了解,直到按Enter按钮之后,程序才会播放声音文件,因为getch()部分需要,然后继续.

It is my understanding that the program will never get to play the sound files until after I press the Enter button, as the getch() part requires before continuing.

但是,即使那部分代码不在乎何时,我按任何键,程序也不会陷入while循环中吗?

However, even if that part of the code does not care when I press anything, wouldn't the program get stuck in the while loop?

推荐答案

winsound.SND_NOWAIT的链接文档指出:

注意:现代Windows平台不支持此标志.

Note: This flag is not supported on modern Windows platforms.

此外,我认为您不了解getch()的工作原理.这是其文档的链接:

Besides that I don't think you understand how getch() works. Here's a link to its documentation:

https://msdn.microsoft.com/en-us/library/078sfkak

这是一个名为kbhit()的相关名称(msvcrt也包含,我在下面使用):

And here's another for a related one named kbhit() (which msvcrt also contains and I use below):

https://msdn.microsoft.com/en-us/library/58w7c94c.aspx

当按下 Enter 键时,以下内容将停止循环(和程序,因为这是其中的唯一内容).请注意,它不会打断任何已经在播放的声音,因为winsound没有提供执行此操作的方法,但是它将停止播放其他声音.

The following will stop the loop (and the program, since that's the only thing in it) when the Enter key is pressed. Note that it doesn't interrupt any single sound already playing because winsound doesn't provide a way to do that, but it will stop any further ones from being played.

from msvcrt import getch, kbhit
import winsound

class StopPlaying(Exception): pass # custom exception

def check_keyboard():
    while kbhit():
        ch = getch()
        if ch in '\x00\xe0':  # arrow or function key prefix?
            ch = getch()  # second call returns the actual key code
        if ord(ch) == 13:  # <Enter> key?
            raise StopPlaying

def play_sound(name, flags=winsound.SND_ALIAS):
    winsound.PlaySound(name, flags)
    check_keyboard()

try:
    while True:
        play_sound("SystemAsterisk")
        play_sound("SystemExclamation")
        play_sound("SystemExit")
        play_sound("SystemHand")
        play_sound("SystemQuestion")

        winsound.MessageBeep()

        play_sound('C:/Users/Admin/My Documents/tone.wav', winsound.SND_FILENAME)

        play_sound("SystemAsterisk")

except StopPlaying:
    print('Enter key pressed')

这篇关于程序运行时,如果按Enter键如何停止音乐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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