Search code examples
pythonkivykivymd

No audio or image on application when ported over


I am a complete beginner when it comes to kivy but I managed to port an over app over to android using the KivyApp to APK Colab. The problem is that the place holder image that I'm using for a logo is not showing and buttons when pressed don't make audio. I have scoured the internet for potential solutions but I can't seem to find the right one, what should I do?

This is my main file

from kivy.lang import Builder
from kivy.core.audio import SoundLoader
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.app import MDApp

Builder.load_file('mainscreen.kv')

class MainScreen(Screen):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    #Engine ON and OFF
    def start_engine(self):
        self.start = SoundLoader.load("C:/Users/Jobert Jr/Desktop/KAEYC/audio-assets/start.ogg")
        self.start.play()

    def stop_engine(self):
        self.start.stop()

    #Alarm ON and OFF
    def play_alarm(self): 
        self.alarm = SoundLoader.load("C:/Users/Jobert Jr/Desktop/KAEYC/audio-assets/alarm.ogg")
        self.alarm.play()
    
    def stop_alarm(self):
        self.alarm.stop()
    
    #Horn
    def play_horn(self):
        self.horn = SoundLoader.load("C:/Users/Jobert Jr/Desktop/KAEYC/audio-assets/horn.ogg")
        self.horn.play()        


class MyScreenManager(ScreenManager):
    pass

class KAEYC(MDApp):
    def build(self):
        screen_manager = MyScreenManager()

        play_screen = MainScreen(name="play")

        screen_manager.add_widget(play_screen)
        return MainScreen()

if __name__ == "__main__":
    KAEYC().run()

This is my .kv file

<MainScreen>
    orientation: 'vertical'
    Image:
        source: 'C:/Users/Jobert Jr/Desktop/KAEYC/image-assets/logo.jpg'
        pos_hint: {'center_x': 0.5,'center_y': 0.8}
        size_hint: .4,.4
    Label:
        text: "KAEYC - ANTI THEFT SYSTEM"
        font_size: 40
        color: (219, 77, 77)
        pos_hint: {'center_x': 0.5,'center_y': 0.6}

    Label:
        text: "CONTROLS"
        font_size: 30
        color: (255, 0, 0)
        pos_hint: {'center_x': 0.5,'center_y': 0.5}

    MDRectangleFlatButton:
        text: "Start"
        on_press: root.start_engine()
        pos_hint: {'center_x': 0.2,'center_y': 0.35}
        size_hint: .1, .1
    MDRectangleFlatButton:
        text:"Stop"
        on_press:root.stop_engine()
        pos_hint:{'center_x': 0.5,'center_y': 0.35}
        size_hint: .1, .1
    MDRectangleFlatButton:
        text:"Lights"
        pos_hint:{'center_x': 0.8,'center_y': 0.35}
        size_hint: .1, .1
    MDRectangleFlatButton:
        text:"Alarm ON"
        on_press:root.play_alarm()
        pos_hint:{'center_x': 0.2,'center_y': 0.2}
        size_hint: .1, .1
    MDRectangleFlatButton:
        text:"Alarm OFF"
        on_press:root.stop_alarm()
        pos_hint:{'center_x': 0.5,'center_y': 0.2}
        size_hint: .1, .1
    MDRectangleFlatButton:
        text:"Horn"
        on_press:root.play_horn()
        pos_hint:{'center_x': 0.8,'center_y': 0.2}
        size_hint: .1, .1

Solution

  • Your code references sound and image files using a path that starts with C:/Users/, but there is no C:/Users/ on an Android device. That is why your problems are happening. Try accessing these resources using a path that is relative to the folder where your main.py resides. For example, try changing:

    source: 'C:/Users/Jobert Jr/Desktop/KAEYC/image-assets/logo.jpg'
    

    to:

    source: 'image-assets/logo.jpg'
    

    This assumes that your code is in the C:/Users/Jobert Jr/Desktop/KAEYC folder.

    You might also need to edit your buildozer.spec file to make sure that these files are included in your final app. Something like:

    source.include_patterns = image-assets/*.jpg