1

I am currently designing an Android music player app. It worked all fine on playing songs. Now, what I want is that when we click on the play button it changes its image to pause and first time clicked it will play the song. When we click on it when its image is pause image, then the song pauses. In simple words, I want to change the on_release action of the button and the image on it when we click on it.

You don't have to worry about how to play and pause. I just want to know how to change the button functionality and the image on it when we click on it.

My current program looks like this:

from kivy.lang import Builder
from kivymd.uix.list import OneLineListItem
from kivymd.app import MDApp
from kivy.core.audio import SoundLoader
import os

helper_string = """
Screen:
    BoxLayout:
        orientation: "vertical"
        ScrollView:
            MDList:
                id: scroll
        MDRoundImageButton:
            id: play_button
            source: "F:/playbutton.png"
            pos_hint: {"center_x":0.5,"center_y":0.5}
            on_release: app.song_player_on_release_play_button()

"""


class MainApp(MDApp):
    def build(self):
        screen = Builder.load_string(helper_string)
        return screen

    def on_start(self):
        for root, dirs, files in os.walk('C:/'):
            for file in files:
                if file.endswith('.mp3'):
                    required_file = file
                    the_location = os.path.abspath(required_file)
                    self.root.ids.scroll.add_widget(OneLineListItem(text=required_file, on_release=self.play_song))
                    # print(required_file)

    def play_song(self, onelinelistitem):
        SongList = []
        # print('play:', onelinelistitem.text)
        the_song_path = onelinelistitem.secondary_text
        SongList.append(the_song_path)
        if len(SongList) == 1:
            sound = SoundLoader.load(SongList[0])
            if sound:
                sound.play()
            print(the_song_path)
        if len(SongList) > 1:
            SoundLoader.load(SongList[0]).stop()
            sound = SoundLoader.load(SongList[1])
            if sound:
                sound.play()
            print(the_song_path)

    def song_player_on_release_play_button(self):
        self.root.ids.play_button.source = "F:/pause button.png"
        self.root.ids.play_button.on_release = self.song_player_on_release_pause_button()

    def song_player_on_release_pause_button(self):
        self.root.ids.play_button.on_release = self.song_player_on_release_play_button()
        self.root.ids.play_button.source = "F:/playbutton.png"


MainApp().run()

In this above code, I thought that if I use the self.root.ids.play_button.on_release, then it will change the on release functionality, but it did not work. Now, I am stuck on this: how can I change the button functionality and the image on it?

I am new to programming and I am using kivy and kivymd in Python to make this app.

If you are testing this, please change the image name or download the images that I originally used:

pause button:
pause button

Play button:
Play button

Again, I want them to change the on_release action on each other, which means that when we click on the pause it prints pause, change the button image to play. Then, when we click on the play it prints play and changes the button image to pause and this goes on forever...

1 Answer 1

1

You don't need to define two functions for this. In one function you can change the play or pause images like below.

    def song_player_on_release_play_button(self, *args):
        if self.root.ids.play_button.source = "F:/playbutton.png":
            self.root.ids.play_button.source = "F:/pause button.png"
        else:
            self.root.ids.play_button.source = "F:/playbutton.png"
Sign up to request clarification or add additional context in comments.

3 Comments

but the main function was the other can you tell me that also?
Didn't understand your question, can you please clarify what you meant by main function? If you are referring to song play/pause functions, then you can add play/pause functions in the same if-else block.
Ok let me make it more clear so it is an else and if statement but this only changes the button source (in short the image on the button) but I want the same thing to also pause a song if it is playing and play the song if it is paused using these buttons and to do this I want to use the kivy SoundLoader library. When we click on the button if the button have the image of play it will change to pause and at the same time the song should also stop and when we click on the pause button the same should happen. And I have figured out how to do this so thanks for your help @amras

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.