nolan
nolan

Reputation: 25

Toggle music On/Off Kivy

Here is the part of my .py file. I want on_state to run each time I toggle a button, but it isn't working. I put the print statements in to test each part, and it will print "ONSTATE!!!" every time I toggle the button. But neither of the 'down' or 'normal' states ever prints.

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()

Here is a look at my .kv file for the project.

<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)

Upvotes: 2

Views: 1092

Answers (1)

ikolim
ikolim

Reputation: 16031

Stop Play Music

Use an ObjectProperty, and sound.unload() method to stop playing music.

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

Pass Toggle Button's State

It is not working because you are passing an object i.e. an instance of the Toggle Button.

Replace

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

with

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

Note

A [Toggle Button][1] has only two states i.e. normal and down. Therefore, you don't need two if statements. Improve performance of your Kivy App by:

Replacing

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

with:

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

Upvotes: 2

Related Questions