Search code examples
androidpythonaudiokivybuildozer

Kivy & Buildozer: How to play audio while Android Application is loading?


Is there a way to play an audio while the Kivy application is loading while running on Android devices? That is play an audio while the presplash image, defined in the buildozer.spec file, is displayed on the screen.


Solution

  • As @inclement answered, this is by editing the Java code executed when the presplash image is displayed. The way to do that is simple.

    1. Open the Android project, created using Buildozer, in Android Studio.
    2. Add the code for playing audio either inside the onCreate() method of the PythonActivity, which is the main activity, or inside the showLoadingScreen() method used for displaying the presplash image while the app is loading.

    Here is the modified onCreate() method for playing audio by passing its location in the device:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.v(TAG, "My oncreate running");
        resourceManager = new ResourceManager(this);
    
        Log.v(TAG, "About to do super onCreate");
        super.onCreate(savedInstanceState);
        Log.v(TAG, "Did super onCreate");
    
        this.mActivity = this;
        Toast.makeText(this, "Working on the Kivy Project in Android Studio", Toast.LENGTH_LONG).show();
        this.showLoadingScreen();
    
        new UnpackFilesTask().execute(getAppRoot());
        MediaPlayer music = new MediaPlayer();
        try {
            music.setDataSource("/storage/emulated/0/music.mp3");
            music.prepare();
            music.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }