我试图在 kivymd 中创建音乐播放器,但它不起作用

I tried to create music player in kivymd but its not working

当我运行它时,它没有得到路径。
它说 b'找不到资源。和 b'GStreamer 错误:状态更改失败并且某些元素无法 post 正确的错误消息以及失败的原因。'

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

"""


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

    def on_start(self):
        for root, dirs, files in os.walk('E:/music/'):
            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))
                    

    
    def play_song(self, onelinelistitem):
        the_song_path = onelinelistitem.text
        if self.sound:
            self.sound.stop()
        self.sound = SoundLoader.load(the_song_path)
        if self.sound:
            self.sound.play()
        print(the_song_path)

    

MainApp().run()

方法os.path.abspath() returns提供的路径的绝对版本,但它不知道该路径在哪个文件夹中。它实际上假定提供的路径在当前工作中目录。要获得正确的路径,请更改:

the_location = os.path.abspath(required_file)
self.root.ids.scroll.add_widget(OneLineListItem(text=required_file, on_release=self.play_song))

至:

the_location = os.path.abspath(os.path.join(root, required_file))
self.root.ids.scroll.add_widget(OneLineListItem(text=the_location, on_release=self.play_song))

通过该更改,您的代码应该可以正常工作。