4

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)

Sanyam Pandey
  • 91
  • 1
  • 7

1 Answers1

2

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.

John Anderson
  • 35,991
  • 4
  • 13
  • 36
  • This worked but now I applied this logic to play the song and it gave me this error [ERROR ] [AudioGstplayer] b'Resource not found.' [ERROR ] [AudioGstplayer] b'GStreamer error: state change failed and some element failed to post a proper error message with the reason for the failure.' [ERROR ] [AudioGstplayer] b'Resource not found.' [ERROR ] [AudioGstplayer] b'GStreamer error: state change failed and some element failed to post a proper error message with the reason for the faliure – Sanyam Pandey Sep 09 '20 at 03:45
  • def play_song(self, onelinelistitem): # print('play:', onelinelistitem.text) the_song_path = os.path.abspath(onelinelistitem.text) sound = SoundLoader.load(the_song_path) if sound: sound.play() print(the_song_path) – Sanyam Pandey Sep 09 '20 at 03:50
  • The `onelinelistitem.text` is just the name of the song file, and `os.path.abspath(onelinelistitem.text)` will not provide the full path to the file. You need to find a way to provide the full path to the file (maybe add a property to the `onelinelistitem`). – John Anderson Sep 09 '20 at 13:02