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.

5 responses 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

  2. sunil shrimali says:

    after building app i cannot able to access music file, i am using os module to fetch all mp3 files

  3. Deepak says:

    Place .py file and music files in same folder.

  4. Izzat says:

    Hi can can u check my code its not working. I worked with kivymd please help me to solve this

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

    def on_start(self):
    for root, dirs, files in os.walk(‘E:/music/’):
    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))

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

    MainApp().run()

  5. Manohar says:

    Thank you very much I just copied the code and it worked. I was searching for this code for weeks thanks again

Leave a Reply

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