- Saved searches
- Use saved searches to filter your results more quickly
- xinshiatubc/kivy-audio-player
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- Audio¶
- Event dispatching and state changes¶
- natcl / audio_player.kv
- Kivy Music Player with Python | Part -1
- Basic layout for Music Player
- Python Kivy How to Play Mp3 Music
- Source code for Python Kivy How to Play Mp3 Music
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
An audio player application written in Python and Kivy language.
xinshiatubc/kivy-audio-player
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
An audio player application written in Python and Kivy language.
Kivy Installation on Windows
- Ensure you have Python 3.7 or previous version installed. Currently Kivy doesn’t support Python 3.8.
- Ensure you have the latest pip and wheel:
python -m pip install --upgrade pip wheel setuptools
python -m pip install docutils pygments pypiwin32 kivy.deps.sdl2 kivy.deps.glew python -m pip install kivy.deps.gstreamer
python -m pip install kivy
That’s it. You should now be able to import kivy in Python.
Installing Other Dependencies
Install matplotlib garden package:
garden install matplotlib
python -m pip install matplotlib
About
An audio player application written in Python and Kivy language.
Audio¶
You should not use the Sound class directly. The class returned by SoundLoader.load() will be the best sound provider for that particular file type, so it might return different Sound classes depending the file type.
Event dispatching and state changes¶
Audio is often processed in parallel to your code. This means you often need to enter the Kivy eventloop in order to allow events and state changes to be dispatched correctly.
You seldom need to worry about this as Kivy apps typically always require this event loop for the GUI to remain responsive, but it is good to keep this in mind when debugging or running in a REPL (Read-eval-print loop).
Changed in version 1.10.0: The pygst and gi providers have been removed.
Changed in version 1.8.0: There are now 2 distinct Gstreamer implementations: one using Gi/Gst working for both Python 2+3 with Gstreamer 1.0, and one using PyGST working only for Python 2 + Gstreamer 0.10.
The core audio library does not support recording audio. If you require this functionality, please refer to the audiostream extension.
Represents a sound to play. This class is abstract, and cannot be used directly.
Use SoundLoader to load a sound.
Fired when the sound is played.
Fired when the sound is stopped.
Deprecated since version 1.3.0: Use source instead.
Returns the current position of the audio file. Returns 0 if not playing.
Get length of the sound (in seconds).
Load the file into memory.
Set to True if the sound should automatically loop when it finishes.
loop is a BooleanProperty and defaults to False.
Pitch of a sound. 2 is an octave higher, .5 one below. This is only implemented for SDL2 audio provider yet.
Most sound providers cannot seek when the audio is stopped. Play then seek.
Filename / source of your audio file.
source is a StringProperty that defaults to None and is read-only. Use the SoundLoader.load() for loading audio.
State of the sound, one of ‘stop’ or ‘play’.
Deprecated since version 1.3.0: Use state instead.
Unload the file from memory.
Volume, in the range 0-1. 1 means full volume, 0 means mute.
class kivy.core.audio. SoundLoader [source] ¶
Load a sound, using the best loader for the given file type.
Load a sound, and return a Sound() instance.
Register a new class to load the sound.
natcl / audio_player.kv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
: |
canvas: |
Clear |
Color: |
rgb: ( 0.2 , 0.2 , 0.2 ) |
Rectangle: |
pos: self .pos |
size: self .width, self .height |
Color: |
rgb: ( 1 , 0 , 0 ) |
Rectangle: |
pos: self .pos |
size: self .width * ( self .value_normalized if self .orientation == ‘ horizontal ‘ else 1 ), self .height * ( self .value_normalized if self .orientation == ‘ vertical ‘ else 1 ) |
: |
padding: 5 , 5 |
spacing: 5 |
size_hint: .75 , .75 |
Button: |
text: » Play « |
on_press: app.buttonCallback() |
size_hint: .60 , 1 |
MySlider: |
id : volume_slider |
step: 1 |
on_value: app.sliderCallback( self .value) |
orientation: ‘ vertical ‘ |
size_hint: .20 , 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
#!/usr/bin/env python |
from kivy . app import App |
from kivy . uix . boxlayout import BoxLayout |
from kivy . clock import Clock |
import os , subprocess |
class MyLayout ( BoxLayout ): |
pass |
class Audio_Player ( App ): |
def getAlsaVolume ( self , * largs ): |
volume_string = subprocess . check_output ([ ‘amixer’ , ‘-c’ , ‘1’ , ‘get’ , ‘Digital’ ])[ 172 : 177 ] |
self . volume = self . parseInt ( volume_string ) |
self . root . ids . volume_slider . value = self . volume |
def sliderCallback ( self , value ): |
os . system ( ‘amixer —quiet -c 1 set Digital %’ . format ( value )) |
print value |
def buttonCallback ( self ): |
os . system ( ‘killall mplayer’ ) |
self . process = subprocess . Popen ( ‘mplayer -nolirc -ao alsa:device=hw=1,0 «»‘ . format ( ‘/home/pi/Music/08 — Strawberry Fields Forever (2009 Digital Remaster).m4a’ ), shell = True , stdout = subprocess . PIPE , stdin = subprocess . PIPE ) |
print ‘button’ |
self . getAlsaVolume () |
def parseInt ( self , string ): |
return int ( » . join ([ x for x in string if x . isdigit ()])) |
def build ( self ): |
self . root = MyLayout () |
Clock . schedule_interval ( self . getAlsaVolume , 1 ) |
return self . root |
if __name__ == ‘__main__’ : |
Audio_Player (). run () |
Kivy Music Player with Python | Part -1
In this tutorial series, we will make a Kivy music player with different features like – progress bar, mute button, play and stop buttons.
So this is Part 1 of the kivy music player series, in this part, we will see, how to make good and attractive UI with the help of kivy and kivyMD, also I will show you how to play local storage songs on a kivy music player by parsing .mp3 songs from our local storage.
So first import all the necessary libraries that we will use in this part 1.
# import os to grab all .mp3 songs from folder # and random to pick a random song from the song list import os import random #import kivy library for UI design import kivy kivy.require('2.0.0') from kivy.app import App from kivymd.uix.relativelayout import MDRelativeLayout from kivymd.uix.button import MDIconButton from kivymd.app import MDApp # import soundloader to load song in kivy from kivy.core.audio import SoundLoader
Basic layout for Music Player
class MyApp(MDApp): def build(self): layout = MDRelativeLayout(md_bg_color = [0,0.5,1,1]) return layout if __name__ == '__main__': MyApp().run()
After running it window size is not perfect for our music player so we will resize our music player window, so first import Window from kivy and resize it.
from kivy.core.audio import SoundLoader from kivy.core.window import Window Window.size = (400,600)
Now create a play and stop button for our music player and also bind the function to play and stop the music.
class MyApp(MDApp): def build(self): layout = MDRelativeLayout(md_bg_color = [0,0.5,1,1]) self.playbutton = MDIconButton(pos_hint=, icon="play.png", on_press = self.playaudio) self.stopbutton = MDIconButton(pos_hint=, icon="stop.png", on_press = self.stopaudio) layout.add_widget(self.playbutton) layout.add_widget(self.stopbutton) return layout if __name__ == '__main__': MyApp().run()
Now create the song list by collecting songs from a local directory.
class MyApp(MDApp): def build(self): layout = MDRelativeLayout(md_bg_color = [0,0.5,1,1]) self.music_dir = 'F:/Project/Parts' music_files = os.listdir(self.music_dir) print(music_files) self.song_list = [x for x in music_files if x.endswith('mp3')] print(self.song_list) self.song_count = len(self.song_list) self.playbutton = MDIconButton(pos_hint=, icon="play.png", on_press = self.playaudio) self.stopbutton = MDIconButton(pos_hint=, icon="stop.png", on_press = self.stopaudio) layout.add_widget(self.playbutton) layout.add_widget(self.stopbutton) return layout if __name__ == '__main__': MyApp().run()
In the above code snippet, we are accessing one path that containing our music files, so first it will make the list of all items that are stored in that particular folder itself, then it will create a list of songs by parsing all files that end with .mp3 present in the folder and finally count a total number of songs stored in the folder.
Now add the play function in MyApp class.
def playaudio(self,obj): self.song_title = self.song_list[random.randrange(0, self.song_count)] print(self.song_title) self.sound = SoundLoader.load('<>/<>'.format(self.music_dir, self.song_title)) self.sound.play()
The above function is selecting the random song from our song list and load that selected song with the help of kivy SoundLoader and finally play a song using play() function.
Now add a function to stop a song.
def stopaudio(self,obj): self.sound.stop()
The above function will simply stop the song with the help of stop() function.
Final Code Up to This Point
import os import random import kivy kivy.require('2.0.0') from kivy.app import App from kivymd.uix.relativelayout import MDRelativeLayout from kivymd.uix.button import MDIconButton from kivymd.app import MDApp from kivy.core.audio import SoundLoader from kivy.core.window import Window Window.size = (400,600) class MyApp(MDApp): def build(self): layout = MDRelativeLayout(md_bg_color = [0,0.5,1,1]) self.music_dir = 'F:/Project/Parts' music_files = os.listdir(self.music_dir) print(music_files) self.song_list = [x for x in music_files if x.endswith('mp3')] print(self.song_list) self.song_count = len(self.song_list) self.playbutton = MDIconButton(pos_hint=, icon="play.png", on_press = self.playaudio) self.stopbutton = MDIconButton(pos_hint=, icon="stop.png", on_press = self.stopaudio) layout.add_widget(self.playbutton) layout.add_widget(self.stopbutton) return layout def playaudio(self,obj): self.song_title = self.song_list[random.randrange(0, self.song_count)] print(self.song_title) self.sound = SoundLoader.load('<>/<>'.format(self.music_dir, self.song_title)) self.sound.play() def stopaudio(self,obj): self.sound.stop() if __name__ == '__main__': MyApp().run()
Python Kivy How to Play Mp3 Music
we need to use SoundLoader class from kivy.core.audio.
Also if you are interested in Python GUI Development with different libraries, you
can check the below links.
Also you can watch the complete video for this article
We are going to create two examples, the first one will be an easy example, the second will be
a little complex example. this is the first example code.
Source code for Python Kivy How to Play Mp3 Music
These are the imports that we have used in the above code, you can see that we have imported
the SoundLoader class from kivy.core.audio module.
In this line of code we have loaded our Mp3 music, make sure that you have added an Mp3
sound in your working directory, as i have already added the mp3 sound.
If you run the code you will see the music is playing.
Now let’s create the second example, in this example we are going to create a button using our
kivy file, and after that we want when a user clicks on the button, we want to play mp3 music.
So first create a python file, iam going to call it kivymusicapp.py, you can see that the first class extends from the FloatLayout, also we have added a method in this class for loading and playing our mp3 sound, because we will connect this method with the button that we create in the .kv file.
Now you need to create a .kv file, iam going to call it musicwindow.kv, make sure that your kv file name should be the same as your main App class, in my case my main window class name is MusicWindow, and my kv file name should be musicwindow.kv. in the kv file we have just created a button, and we have connected the button with the play_music() method that is located in our MyFloatLayout class.