【问题标题】:Python pygame.mixer does not play music infinitelyPython pygame.mixer 不会无限播放音乐
【发布时间】:2014-04-10 05:16:24
【问题描述】:

我想连续播放警报波形文件,直到它检测到按下按钮。在这种情况下我使用键盘中断来尝试。

这是我的脚本:

import pygame

pygame.mixer.init()
pygame.mixer.music.load("beep.wav")
pygame.mixer.music.play(-1)
print "Time to take medicine!"

try:
    while pygame.mixer.music.get_busy() == True:
        continue

except KeyboardInterrupt:
    pygame.mixer.music.stop()

但是它只播放了一次,它会停止并退出脚本。怎么了?

更新: 我设法让它在键盘中断时运行并终止,但我不明白为什么play(-1) 不起作用?

这是我的工作脚本:

import pygame
import sys

pygame.mixer.init()
pygame.mixer.music.load("beep.wav")
pygame.mixer.music.play(-1)
print "Time to take medicine!"

while True:
    try:
        while pygame.mixer.music.get_busy() == True:
            continue

    except KeyboardInterrupt:
        pygame.mixer.music.stop()
        sys.exit()

【问题讨论】:

  • +1。这对我来说也是个谜。这可能是一个错误

标签: python audio pygame


【解决方案1】:

pygame 手册说的

pygame.mixer.music.get_busy()

当音乐流正在播放时返回 True。当音乐 空闲时返回 False。

所以问题是,当声音结束并要重新开始时,这个函数在小间隙中返回 False。这就是您的第二个版本有效的原因。

【讨论】:

    猜你喜欢
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多