有没有一种方法可以 select 来自 FileListView 的歌曲(通过单击),将其加载到 Soundloader 并使用 PLAY 按钮播放?

Is there a way I could select a song from the FileListView (by click), load it to the Soundloader and play it with the PLAY button?

#So this is a simple mp3 player I'm trying to make on python/kivy.我正在使用 FileListView 来显示我的 mp3 文件。有什么方法可以 select FileListView 中的歌曲(通过单击),将其加载到 Soundloader 并使用 PLAY 按钮播放?#

        import os  
        import kivy  
        from kivy.app import App  
        from kivy.uix.button import Button  
        from kivy.uix.boxlayout import BoxLayout  
        from kivy.uix.label import Label  
        from kivy.uix.floatlayout import FloatLayout  
        from kivy.uix.gridlayout import GridLayout  
        from kivy.uix.image import AsyncImage  
        from kivy.uix.filechooser import FileChooserListView  
        from kivy.core.audio import SoundLoader  
        from kivy.loader import Loader  
        from kivy.uix.screenmanager import ScreenManager  


           def callback(instance):  
               print('The button <%s> is being pressed' % instance.text)  


             red = [1, 0, 0, 1]  
             green = [0, 1, 0, 1]  
             blue = [0, 0, 1, 1]  
             purple = [1, 0, 1, 1]  


        class myLayout(BoxLayout):  

             color = [red, green, blue, purple]  

             path = '/home/edward/Music'  


           def __init__(self, **kwargs):  
             super(myLayout, self).__init__(**kwargs)  

             layout = BoxLayout(pos=self.pos, size=self.size,spacing=10)  

            self.add_widget(layout)  

            flc = FileChooserListView(path='/home/edward/Music',filters=['*.mp3'],size=(7, 3))  
            flc.bind(on_selection=self.load_sound)  

             layout.add_widget(flc)  

        # creating the play button  

            play = Button(text="PLAY",valign='center', halign='center',background_color=blue,size_hint=(0.3, 0.5))  

              play.bind(on_press=self.play_pressed)  
              layout.add_widget(play)  

        # creating the stop button  

              stop = Button(text="STOP", valign='center',halign='center',size_hint=(0.3, 0.6),background_color=red)  
              layout.add_widget(stop)  
              stop.bind(on_press=self.stop_pressed)  

        #creating a Label  

             display=Label(text="kiwiAudioPlayer",halign='right', size=(3,5))

             layout.add_widget(display)  

        #displaying an animated image  

         animate = AsyncImage(source='https://i.gifer.com/KNGq.gif',                              allow_stretch=False, anim_delay=0.10)  

              layout.add_widget(animate)  

        #trying to define a load song function  

           def load_sound(self, filename):

                sound = FileChooserListView.selection(self.path)
              with open(os.path.join(self.path, filename[0])):
                if sound is None:
                    sound = SoundLoader.load(self.path)
                if sound.status != 'stop':
                    sound.stop()
   
                    sound.play()
 
         #play button function

           def play_pressed(self, sound, play):  

                sound = SoundLoader.load(self.path)  
                if sound is None:  
                    sound.play()  

      #stop button function  

           def stop_pressed(self, sound, stop):  

                sound = SoundLoader.unload(self.path)  
                sound.stop()  

#main function

      class musicApp(App):  

           def build(self):  

              return myLayout()  

   if __name__ == "__main__":  
           app = musicApp()  
           app.run()  

#所以问题是我如何实际组合 Filelistview 和 Soundloader 模块,以便我可以从列表中单击一首歌曲并立即播放?

稍作修改即可使您的代码正常运行。首先,您对文件选择器 selection 的绑定应该使用 属性 名称,例如:

flc.bind(selection=self.load_sound)    

然后,制作一个实例变量来保存声音可以更容易处理。这是包含这些更改的代码的修改版本:

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.image import AsyncImage
from kivy.uix.filechooser import FileChooserListView
from kivy.core.audio import SoundLoader


def callback(instance):
   print('The button <%s> is being pressed' % instance.text)


red = [1, 0, 0, 1]
green = [0, 1, 0, 1]
blue = [0, 0, 1, 1]
purple = [1, 0, 1, 1]


class myLayout(BoxLayout):

    color = [red, green, blue, purple]
    path = '/home/edward/Music'


    def __init__(self, **kwargs):
        super(myLayout, self).__init__(**kwargs)
        self.sound = None

        layout = BoxLayout(pos=self.pos, size=self.size,spacing=10)

        self.add_widget(layout)

        flc = FileChooserListView(path='/home/edward/Music',filters=['*.mp3'],size=(7, 3))
        flc.bind(selection=self.load_sound)  # bind to the property `selection`

        layout.add_widget(flc)

        # creating the play button

        play = Button(text="PLAY",valign='center', halign='center',background_color=blue,size_hint=(0.3, 0.5))

        play.bind(on_press=self.play_pressed)
        layout.add_widget(play)

        # creating the stop button

        stop = Button(text="STOP", valign='center',halign='center',size_hint=(0.3, 0.6),background_color=red)
        layout.add_widget(stop)
        stop.bind(on_press=self.stop_pressed)

        #creating a Label

        display=Label(text="kiwiAudioPlayer",halign='right', size=(3,5))

        layout.add_widget(display)

        #displaying an animated image

        animate = AsyncImage(source='https://i.gifer.com/KNGq.gif',                              allow_stretch=False, anim_delay=0.10)

        layout.add_widget(animate)

    #trying to define a load song function

    def load_sound(self, filechooser, selection):

        # sound = FileChooserListView.selection(self.path)
        # with open(os.path.join(self.path, filename[0])):
        #     if sound is None:
        #         sound = SoundLoader.load(self.path)
        #     if sound.status != 'stop':
        #         sound.stop()
        if self.sound is not None:
            self.sound.stop()
        self.sound = SoundLoader.load(selection[0])

    #play button function

    def play_pressed(self, button):

        # sound = SoundLoader.load(self.path)
        # if sound is None:
        #     sound.play()
        if self.sound is not None:
            self.sound.play()

    #stop button function

    def stop_pressed(self, button):

        # sound = SoundLoader.unload(self.path)
        # sound.stop()
        if self.sound is not None:
            self.sound.stop()

#main function

class musicApp(App):

   def build(self):

      return myLayout()

if __name__ == "__main__":
       app = musicApp()
       app.run()