I want to make a music player app using kivy and it's material design in python I am successful in finding the .mp3 files and playing them But the problem is that I am not able to find out the way to bind the OneLineListItem to any function (eg. If we click and release the list item, I want to make python to play that song) my code is -:
from kivy.lang import Builder
from kivymd.uix.list import OneLineListItem
from kivymd.app import MDApp
import os
helper_string = """
Screen:
BoxLayout:
orientation: "vertical"
ScrollView:
MDList:
id: scroll
"""
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)
location_list = list(the_location)
song_name = list(required_file)
self.root.ids.scroll.add_widget(OneLineListItem(text=required_file))
# print(required_file)
MainApp().run()
You can see that in this code the for loop iterates over all the files and add them in the scroll view I want to make that for loop to add a on_release action to the list item that plays the song (which song name is on the list item). And every list item should have it's own song name as a text and when we click on the label it will play the song whose name is on the label
And if this on_release action is not possible with the OneLineListItem, then what should I use in that place (eg. A MDFlatRectangleButton, or anything else) and also how should I use it to make my music player work fine (I mean that the functionality of playing the song will be added)
The on_release
action is possible because the OneLineListItem
inherits ButtonBehavior
. So you just need to specify it, like this:
self.root.ids.scroll.add_widget(OneLineListItem(text=required_file, on_release=self.play_song))
Then in your App
, add the play_song()
method:
def play_song(self, onelinelistitem):
print('play:', onelinelistitem.text)
Of course, you will need to add the logic of how to play the mp3
file, and where it is.