简体   繁体   English

Kivy:如何使每个按钮播放不同的声音

[英]Kivy: how to make each buttons play different sound

So I'm writing a drum pad: 所以我正在写一个鼓垫:

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.core.audio import SoundLoader


class GridAction(Button):
    pass


class MakingGrid(GridLayout):

    def __init__(self, *args, **kwargs):
        super(MakingGrid, self).__init__(*args, **kwargs)

        for i in range(16):
            grid_action = GridAction()
            grid_action.bind(on_release=self.button_pressed)
            self.add_widget(grid_action)

    def button_pressed(self, button):
        print('pressed')


class MyApp(App):
    def build(self):
        return MakingGrid(cols=4)


if __name__ == "__main__":
    MyApp().run()

There is a grid of buttons as a result, and the purpose is to make them sound different, but I don't know how to do this. 结果是有一个按钮网格,其目的是使它们听起来有所不同,但我不知道该怎么做。 Please, help. 请帮忙。 Thanks in advance) Also, if you see any disadvantages in this code, please, tell me about them, I`ma beginner. 预先感谢)另外,如果您在此代码中发现任何缺点,请告诉我,我是新手。

A simple way to play audio is with kivy's soundloader . 一种播放音频的简单方法是使用kivy的soundloader

Beyond this, I'm not clear on what your question actually is. 除此之外,我不清楚您的问题是什么。 The general answer is to do something like give each GridAction its own audio_file property holding a filepath to the sound you want, and bind the on_press events to play the sound at this location. 一般的答案是做一些事情,例如为每个GridAction赋予其自己的audio_file属性,该属性持有所需声音的文件路径,然后绑定on_press事件以在此位置播放声音。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM