import os
import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.slider import Slider
from kivy.core.audio import SoundLoader
from kivy.clock import Clock
from kivy.uix.spinner import Spinner


class MusicPlayerApp(App):
    def build(self):
        layout = BoxLayout(orientation='vertical')
        self.music_folder = '/storage/emulated/0/Download'
        self.song_spinner = Spinner(text='Select Song', values=self.get_music_files())
        self.song_spinner.bind(text=self.on_song_select)
        layout.add_widget(self.song_spinner)
        self.play_button = Button(text='Play', on_press=self.play_music)
        layout.add_widget(self.play_button)
        self.stop_button = Button(text='Stop', on_press=self.stop_music)
        layout.add_widget(self.stop_button)
        self.next_button = Button(text='Next', on_press=self.next_song)
        layout.add_widget(self.next_button)
        self.prev_button = Button(text='Previous', on_press=self.previous_song)
        layout.add_widget(self.prev_button)
        self.progress_slider = Slider(min=0, max=1, value=0, step=0.01)
        self.progress_slider.bind(value=self.on_progress_change)
        layout.add_widget(self.progress_slider)
        self.current_song = None
        self.song_index = 0
        return layout

    def get_music_files(self):
        files = os.listdir(self.music_folder)
        music_files = [f for f in files if f.endswith('.mp3')]
        return music_files

    def on_song_select(self, spinner, text):
        self.current_song = SoundLoader.load(os.path.join(self.music_folder, text))
        self.play_button.disabled = False

    def play_music(self, button):
        if self.current_song:
            self.current_song.play()
            Clock.schedule_interval(self.update_progress_slider, 0.1)
            self.play_button.disabled = True
            self.stop_button.disabled = False

    def stop_music(self, button):
        if self.current_song:
            self.current_song.stop()
            self.progress_slider.value = 0
            self.play_button.disabled = False
            self.stop_button.disabled = True

    def next_song(self, button):
        self.song_index = (self.song_index + 1) % len(self.song_spinner.values)
        self.song_spinner.text = self.song_spinner.values[self.song_index]
        self.play_music(None)  # Auto-play the next song

    def previous_song(self, button):
        self.song_index = (self.song_index - 1) % len(self.song_spinner.values)
        self.song_spinner.text = self.song_spinner.values[self.song_index]
        self.play_music(None)  # Auto-play the previous song

    def on_progress_change(self, slider, value):
        if self.current_song:
            self.current_song.seek(value * self.current_song.length)

    def update_progress_slider(self, dt):
        if self.current_song:
            self.progress_slider.value = self.current_song.get_pos() / self.current_song.length

            if not self.current_song.state == 'play':
                # Automatically switch to the next song when the current song ends
                self.next_song(None)

    def on_stop(self):
        if self.current_song:
            self.current_song.stop()


if __name__ == '__main__':
    MusicPlayerApp().run()
Добавь функцию выключения предыдущей песни при переключении