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 kivy.network.urlrequest 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 
b.play() # 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 main.py is as follows:,

from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.app import App
from kivy.network.urlrequest import UrlRequest
from kivy.core.audio 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])


url="http://106.14.124.112:8080/download_voice_en/apple.wav"

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

#:import utils kivy.utils
<test>:
    name:"test"
    BoxLayout:
        Button:
            text: "download"
            on_release:root.buttonClicked()
        Button:
            text: "apple"
            on_release:root.sound()
                        """)

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

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

for screen in screens:  # Generate containers for each window
    sm.add_widget(screen)


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


ScreenApp().run()



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

# (list) Permissions
android.permissions = INTERNET,WRITE_EXTERNAL_STORAGE,READ_EXTERNAL_STORAGE

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