Playing MP3 File in Kivy Using Python

Learn how to play MP3 file in Python using Kivy.

Play MP3 File in Kivy Using SoundLoader in Python

Here we are going to play mp3 file in kivy using Python Programming Language. In kivy mp3 sound files are implemented using the SoundLoader module which is present in kivy.core.audio. It is imported as follows

from kivy.core.audio import SoundLoader

The SoundLoader consists of various operations to be performed on sound files as follows

  • load(): This will help us to load the mp3 file.
  • play(): This is used to play the mp3 file.
  • stop(): It is used to stop playing an mp3 file.

The following code gives a complete implementation of playing a sound file.

import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.core.audio import SoundLoader
class music(App):
    sound=SoundLoader.load('sumpony1.mp3')
    def build(self):
        return Label(text="music playing")
    if sound:
        sound.play()
music().run()

Output:

Play MP3 File in Kivy Using SoundLoader in Python

We get the output as a kivy window with description music playing and the mp3 file gets played as soon as the kivy window is opened.

from kivy.app import App

This will help us to create a window on which we will perform our operations.

from kivy.core.audio import SoundLoader

It will import the modules that are necessary for executing sound files in kivy.

sound=SoundLoader.load('sumpony1.mp3')

This will load the mp3 file and we are assigning this to an object called sound.

if sound:
        sound.play()

Here, the sound object returns True only if it’s a sound file and we are playing sound using play() with the help of a sound object.

One response to “Playing MP3 File in Kivy Using Python”

  1. Lemarchand says:

    Hello, this was what I’ve been looking for. I mean I have the ideas but you code it.
    Now I have to gain more practice with kivy and try to make something more complicated.
    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *