SoundLoader将歌曲与kivy,python重叠如何在python kivy中代替其他歌曲播放歌曲?

问题描述

我一直在使用python和kivymd开发应用程序
这是一个简单的音乐播放器应用程序,可在手机中查找所有音乐并将其显示为列表
当我们点击歌曲名称时,它将播放歌曲 但是问题是歌曲重叠,然后继续重叠
我想要的是,当我们单击任何其他歌曲时,上一首歌曲停止并且被点击的歌曲开始播放

我以前的代码中没有停止功能:

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):
        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)
                    self.root.ids.scroll.add_widget(OneLineListItem(text=required_file,on_release=self.play_song))
                    # print(required_file)

    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)


MainApp().run()

我在新代码中尝试使用列表进行简单修复的方法也无法正常工作-:

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):
        screen = Builder.load_string(helper_string)
        return screen

    def on_start(self):
        for root,onelinelistitem):
        SongList = []
        # print('play:',onelinelistitem.text)
        the_song_path = onelinelistitem.secondary_text
        SongList.append(the_song_path)
        if len(SongList) == 1:
            sound = SoundLoader.load(SongList[0])
            if sound:
                sound.play()
            print(the_song_path)
        if len(SongList) > 1:
            SoundLoader.load(SongList[0]).stop()
            sound = SoundLoader.load(SongList[1])
            if sound:
                sound.play()
            print(the_song_path)


MainApp().run()

?是的,我知道这是一个非常糟糕的修复程序

所以任何人都可以帮助我解决这个问题

非常感谢您

解决方法

您的SongList是方法play_song()中的局部变量,并且每次调用[]时都设置为play_song()。所以条件:

if len(SongList) > 1:

永远不会是真的。一种简单的处理方法是定义一个实例变量,该实例变量保存当前播放的声音,将其称为self.sound。通过build()方法对其进行初始化:

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

并在play_song()方法中使用它:

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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...