【问题标题】:I need a way to stop a function from running at the push of a button我需要一种方法来阻止功能在按下按钮时运行
【发布时间】:2021-10-11 18:32:53
【问题描述】:

我已经为此工作了好几个小时,并且尝试了从break 中的if 语句到创建索引变量的所有方法。我似乎找不到阻止声音/音乐播放的方法。我的目标是按“Q”并停止函数,然后 while 循环开始另一个 while 循环。

import keyboard
import time
from playsound import playsound

#this handles the name that is displayed and makes it easier to input
def Song(type,opt):
        time = 1
        print(opt)
        playsound(type)

print('''Enter Your Music Selection:
O) Overworld
F) Fighting
B) Boss-Strong Enemy
V) Victory
I) Inside-Taven-Shop
D) Dungion
L) Lose-Death
    ''')

while True:
    while keyboard.is_pressed('O'):
        Song('MusicStuff/test0.wav','Overworld Theme')

    while keyboard.is_pressed('F'):
        Song('MusicStuff/TrashLine.wav','Fighting Theme')

【问题讨论】:

标签: python python-3.x python-playsound


【解决方案1】:

Playsound 由一个函数组成,它只播放声音,不做任何其他事情。这就是您无法“停止”该功能的原因。

如果您想要更多的交互性,您必须使用不同的库。你可以使用pyaudio。

看看这篇文章。

stopping audio with playsound module

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 2021-04-21
    • 1970-01-01
    • 2016-07-23
    • 1970-01-01
    相关资源
    最近更新 更多