【问题标题】:I tried to create music player in kivymd but its not working [closed]我试图在 kivymd 中创建音乐播放器,但它不工作 [关闭]
【发布时间】:2020-11-01 15:04:01
【问题描述】:

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

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

【问题讨论】:

    标签: kivy kivymd


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多