- Kivy Tutorial
- Kivy - Home
- Kivy Basics
- Kivy - Getting Started
- Kivy - Installation
- Kivy - Architecture
- Kivy - File Syntax
- Kivy - Applications
- Kivy - Hello World
- Kivy - App Life Cycle
- Kivy - Events
- Kivy - Properties
- Kivy - Inputs
- Kivy - Behaviors
- Kivy Buttons
- Kivy - Buttons
- Kivy - Button Events
- Kivy - Button Colors
- Kivy - Button Size
- Kivy - Button Position
- Kivy - Round Buttons
- Kivy - Disabled Buttons
- Kivy - Image Button
- Kivy Widgets
- Kivy - Widgets
- Kivy - Label
- Kivy - Text Input
- Kivy - Canvas
- Kivy - Line
- Kivy - Checkbox
- Kivy - Dropdown List
- Kivy - Windows
- Kivy - ScrollView
- Kivy - Carousel
- Kivy - Slider
- Kivy - Images
- Kivy - Popup
- Kivy - Switch
- Kivy - Spinner
- Kivy - Splitter
- Kivy - Progress Bar
- Kivy - Bubble
- Kivy - Tabbed Panel
- Kivy - Scatter
- Kivy - Accordion
- Kivy - File Chooser
- Kivy - Color Picker
- Kivy - Code Input
- Kivy - Modal View
- Kivy - Toggle Button
- Kivy - Camera
- Kivy - Tree View
- Kivy - reStructuredText
- Kivy - Action Bar
- Kivy - Video Player
- Kivy - Stencil View
- Kivy - VKeyboard
- Kivy - Touch Ripple
- Kivy - Audio
- Kivy - Videos
- Kivy - Spelling
- Kivy - Effects
- Kivy - Input Recorder
- Kivy - OpenGL
- Kivy - Text
- Kivy - Text Markup
- Kivy - Settings
- Kivy Layouts
- Kivy - Layouts
- Kivy - Float Layout
- Kivy - Grid Layouts
- Kivy - Box Layouts
- Kivy - Stack Layout
- Kivy - Anchor Layout
- Kivy - Relative Layout
- Kivy - Page Layout
- Kivy - Recycle Layout
- Kivy - Layouts in Layouts
- Kivy Advanced Concepts
- Kivy - Configuration Object
- Kivy - Atlas
- Kivy - Data Loader
- Kivy - Cache Manager
- Kivy - Console
- Kivy - Animation
- Kivy - Multistroke
- Kivy - Clock
- Kivy - SVGs
- Kivy - UrlRequest
- Kivy - Clipboard
- Kivy - Factory
- Kivy - Gesture
- Kivy - Language
- Kivy - Graphics
- Kivy - Drawing
- Kivy - Packaging
- Kivy - Garden
- Kivy - Storage
- Kivy - Vector
- Kivy - Utils
- Kivy - Inspector
- Kivy - Tools
- Kivy - Logger
- Kivy - Framebuffer
- Kivy Applications and Projects
- Kivy - Drawing App
- Kivy - Calculator App
- Kivy - Stopwatch App
- Kivy - Camera Handling
- Kivy - Image Viewer
- Kivy - Bezier
- Kivy - Canvas Stress
- Kivy - Circle Drawing
- Kivy - Widget Animation
- Kivy - Miscellaneous
- Kivy Useful Resources
- Kivy - Quick Guide
- Kivy - Useful Resources
- Kivy - Discussion
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.
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.
To Continue Learning Please Login
Login with Google