python kivy app development and playing sound music

Keywords: Python Back-end

Using kivy to realize sound playback seems simple, but there are pits. It is also a summary of digging pits after reading a large number of Internet data.

You can use his SoundLoader component to play audio. You can play sound in two lines. If you just run on the PC, just copy the following code

PC playback

from import UrlRequest
path="./sound/apple.wav" # It can be a local file or a web address http://XXX/apple.wav , or mp3
b=SoundLoader.load(filename=path) # load file # Play sound

Mobile terminal plays local files

However, if you want to play local sound on Android, you need to pay attention to the following problems:
1. At present, Kivy only supports wav format

2. When packaging APK, you should pay attention to modifying relevant parameters. Enter buildozer init in the packaging environment to generate the initialization parameter table. You don't know how to package with buildozer. Please see the tutorial of buildozer packaging APK first. Find the following parameters and add wav. In Linux environment, vim can be used to open files for editing

3. The following two ends should be changed. Don't omit. These two paragraphs are not connected

# (list) Source files to include (let empty to include all the files)
source.include_exts = py,png,jpg,kv,atlas,wav
# (list) List of inclusions using pattern matching
source.include_patterns = assets/*,images/*.png,sound/*.wav

Mobile terminal plays web page audio / server audio

However, if you want to play the web page / server back-end sound on Android, you need to pay attention to the following problems:

Kivy's player does not support streaming media!!!!, So you need to download it and play it again

Here I created a download button and a play button. Click play to download, wait a while, and then click the play button to play the sound. The effect is as follows

python main program code for reference
Here you will see Import os and from android.permissions import. Note that Android does not need to be installed. It comes with Buildozer and P4a and can only take effect on the Android side. Therefore, it is necessary to judge the system environment first

In addition, the spec file needs to be modified

The main program code of is as follows:,

from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from import App
from import UrlRequest
from import SoundLoader
from kivy.utils import platform
#import requests
import os
if platform == "android":
    from android.permissions import request_permissions, Permission
    request_permissions([Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE])


# Build window button, a download button, a play button
kv = Builder.load_string("""

#:import utils kivy.utils
            text: "download"
            text: "apple"

class test(Screen):
    def buttonClicked(self):
        a= UrlRequest(url, file_path='apple.wav')
        print("done download")
    def sound(self):
        print("done sound")

sm = ScreenManager()  # Create window manager
screens = [test(name="test")]

for screen in screens:  # Generate containers for each window

class ScreenApp(App):
    def build(self):
        return sm


The permission needs to be modified in the spec file to obtain network and read-write permissions

# (list) Permissions

Posted by myfafa on Thu, 11 Nov 2021 01:25:33 -0800