Kivy - Audio



Kivy framework provides the Sound class that handles functions like loading audio files, playing and stopping the playback. Sound class is one of the core classes, defined in the "kivy.core.audio" module.

It is not advised to instantiate the Sound object directly. Instead, use the SoundLoader function as shown below −

from kivy.core.audio import SoundLoader
sound = SoundLoader.load('test.mp3')

Audio playback is handled by Gstreamer implementations: using Gi/Gst and PyGST. Gi/GST works for both Python 2+3 with Gstreamer 1.0, while PyGST working only for Python 2 + Gstreamer 0.10.

Note that the core audio library does not support recording audio. If you require this functionality, please refer to the audiostream extension.

The Sound object has the following important properties/methods −

Sr.No Properties/Methods & Description
1 load()

Load the file into memory.

2 play()

Play the file.

3 stop()

Stop playback.

4 unload()

Unload the file from memory.

5 seek(position)

Go to the <position> (in seconds).

6 get_pos()

Returns the current position of the audio file. Returns 0 if not playing.

7 length

Get length of the sound (in seconds).

8 loop

Set to True if the sound should automatically loop when it finishes.

9 source

Filename / source of your audio file.

10 state

State of the sound, one of 'stop' or 'play'.

Let us use the Sound object and build a simple audio player in Kivy. The application window houses a label that shows the present status of the player, i.e., whether it is playing or stopped, and two buttons to control the playback. A button on the left is captioned 'Play' to start with.

Kivy Audio

When clicked, it loads the sound object from a mp3 file, calls play() method, changes label caption to 'Playing' and enables the pause button, and its caption changes to 'Stop'.

When the left button is clicked while its caption is 'Stop', the playback is stopped, restores the label caption and disables the pause button.

When you hit the Pause button, the current position of audio file is stored in pos variable, the button caption changes to Resume. When it is clicked, the play position is retrieved by calling seek() method.

Example

Here is the complete code −

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.core.audio import SoundLoader
from kivy.core.window import Window

Window.size = (720, 350)

class audiodemoapp(App):
   def build(self):
      layout = GridLayout(cols=1, padding=10)

      self.l1 = Label(
         text='Press Start to Play',
         font_size=40, color=[.8, .6, .4, 1]
      )
      layout.add_widget(self.l1)
      box = BoxLayout(orientation='horizontal')
      self.button1 = Button(text="Play", font_size=32)
      self.button2 = Button(
         text='Pause', font_size=32, disabled=True
      )
      box.add_widget(self.button1)
      box.add_widget(self.button2)
      layout.add_widget(box)
      self.button1.bind(on_press=self.start_stop)
      self.button2.bind(on_press=self.pause_resume)

      return layout

   def start_stop(self, event):
      if self.button1.text == 'Play':
         self.l1.text = 'Playing'
         self.button1.text = 'Stop'
         self.sound = SoundLoader.load('sample.mp3')
         self.pos = 0
         self.button2.disabled = False
         self.sound.play()
      else:
         if self.button1.text == 'Stop':
            self.l1.text = 'Press to Play'
            self.button1.text = 'Play'
            self.sound.unload()
            self.button2.disabled = True
            self.pos = 0

   def pause_resume(self, event):
      if self.button2.text == 'Pause':
         self.button2.text = 'Resume'
         self.l1.text == 'Paused'
         self.pos = self.sound.get_pos()
         print(self.pos)
         self.sound.stop()
      else:
         if self.button2.text == 'Resume':
            self.l1.text = 'Playing'
            self.button2.text = 'Pause'
            print(self.pos)
            self.sound.seek(self.pos)
            self.sound.play()
            
audiodemoapp().run()

Output

The following figure shows the status caption set to 'Playing' and pause button is enabled. The Pause button toggles between Pause and Resume states.

Kivy Audio Play
Advertisements