Python Kivy How to Play Mp3 Music

In this Python Kivy article we are going to learn How to Play Mp3 Music in Kivy, so for this

we need to use SoundLoader class from kivy.core.audio.

 

 

Also if you are interested in Python GUI Development with different libraries, you

can check the below links.

 

1: PyQt5 GUI Development Tutorials

2: Pyside2 GUI Development Tutorials 

3: wxPython GUI Development Tutorials

4: TKinter GUI Development Tutorials 

 

 

 

 

Also you can watch the complete video for this article

 

 

 

 

 

We are going to create two examples, the first one will be an easy example, the second will be

a little complex example. this is the first example code.

 

 

 

 

Source code for Python Kivy How to Play Mp3 Music 

 

 

 

 

 

These are the imports that we have used in the above code, you can see that we have imported

the SoundLoader class from kivy.core.audio module.

 

 

 

 

In this line of code we have loaded our Mp3 music, make sure that you have added an Mp3

sound in your working directory, as i have already added the mp3 sound.

 

 

 

 

If you run the code you will see the music is playing.

Python Kivy How to Play Mp3 Music
Python Kivy How to Play Mp3 Music

 

 

 

 

Now let’s create the second example, in this example we are going to create a button using our

kivy file, and after that we want when a user clicks on the button, we want to play mp3 music.

 

 

 

 

So first create a python file, iam going to call it kivymusicapp.py, you can see that the first class extends from the FloatLayout, also we have added a method in this class for loading and playing our mp3 sound, because we will connect this method with the button that we create in the .kv file.

 

 

 

 

Now you need to create a .kv file, iam going to call it musicwindow.kv, make sure that your kv file name should be the same as your main App class, in my case my main window class name is MusicWindow, and my kv file name should be musicwindow.kv. in the kv file we have just created a button, and we have connected the button with the play_music() method that is located in our MyFloatLayout class.

 

 

 

 

 

 

So run the complete code this will be the result, if you click on the button, the mp3 music

will be played.

Python Kivy How to Play Mp3 Music
Python Kivy How to Play Mp3 Music

 

 

 

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

2 thoughts on “Python Kivy How to Play Mp3 Music”

  1. Good, the first thing is to thank you for your
    videos has helped me learn. I would like to ask you a question about printing with QPrintPreviewDialog. As I could avoid the 20mm margins in the prints, I would like less than a 5mm. Thanks a greeting

Comments are closed.

Share via
Copy link
Powered by Social Snap
×