Python - Pygame - 获取特定声音是否正在播放

标签 python audio pygame

Pygame 的混合器模块有 pygame.mixer.get_busy 返回一个简单的 bool 值。
我的问题是我不断播放声音,例如爆炸声和枪声,需要知道何时播放特定声音以防止游戏对话重叠。

我考虑过制作一个当前正在播放的对话列表,创建一个计时器,在每个声音被触发时倒计时,但这需要我在主游戏循环中添加音效(我的处理声音的模块)更新。< br/> 这看起来很困惑,就像一个巨大的减速。

有没有更简洁的方法来做到这一点?

最佳答案

您可以使用 channel 对象。

在 channel 中播放特定类型的音频并检查声音是否正在播放。

import pygame

def main(filepath):
    pygame.mixer.init()

    # If you want more channels, change 8 to a desired number. 8 is the default number of channel

    pygame.mixer.set_num_channels(8)

    # This is the sound channel
    voice = pygame.mixer.Channel(5)

    sound = pygame.mixer.Sound(filepath)

    voice.play(sound)

    if voice.get_busy():
        #Do Something

关于Python - Pygame - 获取特定声音是否正在播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14432851/

相关文章:

python - Pygame 无响应显示

python - 如何将输入文件数据转换为日期格式?

python - 如何使用 Orange 离散化存储在 numpy 数组中的数据?

python - 插入排序 Python

c++ - 编写音频源过滤器用作 Lync 麦克风

python - 为什么说模块pygame没有init成员?

python - 在pygame中堆叠不同类型的透明度

python - XAMPP - 在网页上执行 Python 脚本

php - 流式传输 MP3 会切断最后 1-2 秒的音频

c# - Crossfading - 获取音频 C# .NET Compact Framework 的最佳方式?