Working with audio
Nowadays, the audio integration in our app is vital. You could not realize a video game without audio or an app that does not use multimedia. We will create a sample with just one button which when pressed plays an audio.
Getting ready
We need an audio file in this recipe in the traditional audio formats (mp3, mp4, wav, wma, b-mtp, ogg, spx, midi). If you do not have any, you always can get one from sites such as https://www.freesound.org.
How to do it…
We will use a simple Python file with just one widget to play the audio file. To complete the recipe, let's follow these steps:
Import the usual
kivy
package.Import the
SoundLoader
package.Define the
MyW()
class.Define the
__init__()
method.Create a button with the label
Play
.Bind the press action with the
press()
method.Add the widget to the app.
Define the
press()
method.Call the
SoundLoader.sound()
method for your audio file.Play it with the
play()
method:import kivy kivy.require('1.9.0') from kivy.app import App from kivy...