Kivy Music Player with Python | Part -1

In this tutorial series, we will make a Kivy music player with different features like – progress bar, mute button, play and stop buttons.

So this is Part 1 of the kivy music player series, in this part, we will see, how to make good and attractive UI with the help of kivy and kivyMD, also I will show you how to play local storage songs on a kivy music player by parsing .mp3 songs from our local storage.

So first import all the necessary libraries that we will use in this part 1.

# import os to grab all .mp3 songs from folder
# and random to pick a random song from the song list
import os
import random
#import kivy library for UI design
import kivy
kivy.require('2.0.0')
from kivy.app import App
from kivymd.uix.relativelayout import MDRelativeLayout
from kivymd.uix.button import MDIconButton
from kivymd.app import MDApp
# import soundloader to load song in kivy
from kivy.core.audio import SoundLoader

Basic layout for Music Player

class MyApp(MDApp):
    
    def build(self):
    	layout = MDRelativeLayout(md_bg_color = [0,0.5,1,1])
    	return layout
if __name__ == '__main__':
    MyApp().run()
Music player basic layout output

After running it window size is not perfect for our music player so we will resize our music player window, so first import Window from kivy and resize it.

from kivy.core.audio import SoundLoader
from kivy.core.window import Window
Window.size = (400,600)
kivy music player basic layout changed size

Now create a play and stop button for our music player and also bind the function to play and stop the music.

class MyApp(MDApp):
    
    def build(self):
    	layout = MDRelativeLayout(md_bg_color = [0,0.5,1,1])
    	
    	self.playbutton = MDIconButton(pos_hint={'center_x':0.4, 'center_y':0.05},
                                       icon="play.png",
                                       on_press = self.playaudio)
        
		self.stopbutton = MDIconButton(pos_hint={'center_x':0.55, 'center_y':0.05},
		                                       icon="stop.png",
		                                       on_press = self.stopaudio)
		layout.add_widget(self.playbutton)
		layout.add_widget(self.stopbutton)
    	return layout
if __name__ == '__main__':
    MyApp().run()

Now create the song list by collecting songs from a local directory.

local directory folder content
class MyApp(MDApp):
    
    def build(self):
    	layout = MDRelativeLayout(md_bg_color = [0,0.5,1,1])
    	self.music_dir = 'F:/Project/Parts'
        music_files = os.listdir(self.music_dir)
        
        print(music_files)
        
        self.song_list = [x for x in music_files if x.endswith('mp3')]
        print(self.song_list)
        
        self.song_count = len(self.song_list)
    	
    	self.playbutton = MDIconButton(pos_hint={'center_x':0.4, 'center_y':0.05},
                                       icon="play.png",
                                       on_press = self.playaudio)
        
		self.stopbutton = MDIconButton(pos_hint={'center_x':0.55, 'center_y':0.05},
		                                       icon="stop.png",
		                                       on_press = self.stopaudio)
		layout.add_widget(self.playbutton)
		layout.add_widget(self.stopbutton)
    	return layout
if __name__ == '__main__':
    MyApp().run()

In the above code snippet, we are accessing one path that containing our music files, so first it will make the list of all items that are stored in that particular folder itself, then it will create a list of songs by parsing all files that end with .mp3 present in the folder and finally count a total number of songs stored in the folder.

Now add the play function in MyApp class.

def playaudio(self,obj):
   self.song_title = self.song_list[random.randrange(0, self.song_count)]
   print(self.song_title)
   self.sound = SoundLoader.load('{}/{}'.format(self.music_dir, self.song_title))
   self.sound.play()

The above function is selecting the random song from our song list and load that selected song with the help of kivy SoundLoader and finally play a song using play() function.

Now add a function to stop a song.

def stopaudio(self,obj):
    self.sound.stop() 

The above function will simply stop the song with the help of stop() function.

Final Code Up to This Point

import os
import random
import kivy
kivy.require('2.0.0')
from kivy.app import App
from kivymd.uix.relativelayout import MDRelativeLayout
from kivymd.uix.button import MDIconButton
from kivymd.app import MDApp
from kivy.core.audio import SoundLoader
from kivy.core.window import Window
Window.size = (400,600)
class MyApp(MDApp):
    
    def build(self):
        layout = MDRelativeLayout(md_bg_color = [0,0.5,1,1])
        self.music_dir = 'F:/Project/Parts'
        music_files = os.listdir(self.music_dir)
        
        print(music_files)
        
        self.song_list = [x for x in music_files if x.endswith('mp3')]
        print(self.song_list)
        
        self.song_count = len(self.song_list)
        self.playbutton = MDIconButton(pos_hint={'center_x':0.4, 'center_y':0.05},
                                       icon="play.png",
                                       on_press = self.playaudio)
        
        self.stopbutton = MDIconButton(pos_hint={'center_x':0.55, 'center_y':0.05},
                                       icon="stop.png",
                                       on_press = self.stopaudio)
        layout.add_widget(self.playbutton)
        layout.add_widget(self.stopbutton)        
        
        return layout
    def playaudio(self,obj):
        self.song_title = self.song_list[random.randrange(0, self.song_count)]
        print(self.song_title)
        self.sound = SoundLoader.load('{}/{}'.format(self.music_dir, self.song_title))
        self.sound.play()
    def stopaudio(self,obj):
        self.sound.stop()
    
if __name__ == '__main__':
    MyApp().run()

Output:

Play and Stop button in kivy music player

Check out 3 Best Plugin To Improve Python Code Quality.

1 thought on “Kivy Music Player with Python | Part -1”

  1. Pingback: 2 Python Library to Create Amazing Mobile Apps: Kivy vs BeeWare - ProgrammingFever

Comments are closed.

Shopping Basket