【问题标题】:Toggle music On/Off Kivy切换音乐开/关 Kivy
【发布时间】:2023-03-09 18:57:01
【问题描述】:

这是我的 .py 文件的一部分。每次切换按钮时,我都希望 on_state 运行,但它不起作用。我把打印语句放入测试每个部分,它会打印“ONSTATE!!!”每次我切换按钮。但是“向下”或“正常”状态都不会打印出来。

class MusicScreen(Screen):

enter = ObjectProperty(None)
text_input = ObjectProperty(None)
stop = ObjectProperty(None)
musicbutton = ToggleButton()

class MainApp(App):

def build(self):
    return presentation

def on_state(self, state, filename):
    print("ONSTATE!!!")
    sound = SoundLoader.load('/users/nolandonley/desktop/MP3/mus/' + filename + '.m4a')
    if state == "down":
        print("DOWN")
        sound.volume = .5
        sound.play()
    if state == "normal":
        print("normal")
        sound.stop()

这是我的项目的 .kv 文件。

<MusicScreen>:

text_input: text_input
id: "music"
name: "music"
BoxLayout:
    size: root.size
    pos: root.pos
    orientation: "vertical"
    FileChooserListView:
        id: filechooser
        rootpath: "/Users/nolandonley/desktop/MP3/mus"
        on_selection: text_input.text = self.selection and self.selection[0] or ''

    TextInput:
        id: text_input
        size_hint_y: None
        height: 50
        multiline: False
    ToggleButton:
        #pos: 44, 30
        size_hint: 1, .2
        text: "Play/Stop By File"
    ToggleButton:
        id: musicbutton
        #pos: 44, 30
        size_hint: 1, .2
        text: "Play/Stop By Title"
        on_state: app.on_state(self, text_input.text)

【问题讨论】:

    标签: python-3.x kivy togglebutton


    【解决方案1】:

    停止播放音乐

    使用 ObjectPropertysound.unload() 方法停止播放音乐。

    main.py

    ​​>
    class MainApp(App):
        sound = ObjectProperty(None, allownone=True)
    
        def build(self):
            return ScreenManagement()
    
        def on_state(self, state, filename):
            print("ONSTATE!!!")
            print("\tstate=", state)
    
            if self.sound is None:
                self.sound = SoundLoader.load(filename)
    
            # stop the sound if it's currently playing
            if self.sound.status != 'stop':
                self.sound.stop()
    
            if state == "down":
                self.sound.volume = .5
                self.sound.play()
            else:   # if state == "normal":
                if self.sound:
                    self.sound.stop()
                    self.sound.unload()
                    self.sound = None
    

    传递切换按钮的状态

    它不起作用,因为您正在传递一个对象,即切换按钮的一个实例。

    替换

    on_state: app.on_state(self, text_input.text)
    

    on_state: app.on_state(self.state, text_input.text)
    

    注意

    [Toggle Button][1] 只有两种状态,即 正常down。因此,您不需要两个 if 语句。通过以下方式提高 Kivy 应用的性能:

    更换

    if state == "down":
        print("DOWN")
        sound.volume = .5
        sound.play()
    if state == "normal":
        print("normal")
        sound.stop()
    

    与:

    print(state)
    if state == "down":
        sound.volume = .5
        sound.play()
    else:   # if state == "normal":
        sound.stop()
    

    【讨论】:

    • 我已经进行了更改,但是当我将按钮切换到正常状态时,音乐并没有停止。您知道为什么会发生这种情况和/或我该如何解决吗?
    • 停止播放音乐请参考更新后的帖子。
    猜你喜欢
    • 2017-07-23
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多