3

I used the BeeWare environment to create a simple MahJong game (find & click pairs to remove them) using Python (with Toga as layout tool) for Android.

Now I would like to have some buttons give a "click sound" when pressed:

Anyone have a helping hint (or even working example)?

clemsam lang
  • 440
  • 3
  • 11

1 Answers1

2

If you're using Briefcase 0.3.10 or newer (which uses Chaquopy to support Python on Android), then you could use the Chaquopy Python API to play audio files using SoundPool.

For example, the code from this answer could be written in Python as follows:

from android.media import AudioManager, SoundPool
from os.path import dirname, join

soundPool = SoundPool(5, AudioManager.STREAM_MUSIC, 0)
soundId = soundPool.load(join(dirname(__file__), "filename.mp3"), 1)
soundPool.play(soundId, 1, 1, 0, 0, 1)

This will play the file "filename.mp3" from the same directory as the Python source file.

mhsmith
  • 6,675
  • 3
  • 41
  • 58
  • Thanx for your hint which I will follow shortly. Unfortunately I cannot get the current briefcase installation to RUN on my Macos Android emulator (= crashes after splash screen, dev & Macos run work) and am about to find out where the fault lies (> Python version, Macos 10.13.6, SDK version …). Funny that the apk file transferred to my actual device (amazon-FireHD-10) has no problem at all … Are you aware of minimum requirements for briefcase/chaquopy? … I seem to be unable to find definite values, which IS a pity. – clemsam lang Nov 02 '22 at 17:49
  • Okay, found some reasons for crashes: my emulator's config.ini says "abi.type=x86" so I added "splits {abi { … … include 'x86' universalApk true}}" (found here: "http://androidlad.blogspot.com/2016/10/installfailednomatchingabis-when.html") to build.gradle. Next, in "ndk" part I added a value of: 'abiFilters "x86"' and skipped rest. Still I got: "Unable to install APK [myProject]/…/app-debug.apk on emulator-5554" which I healed by renaming the outputs' "app-x86-debug.apk" to the expected "app-debug.apk" – now "briefcase run android" works like a charm. (Will look into SoundPool asap … ) – clemsam lang Nov 03 '22 at 09:56
  • 1
    Google has stopped releasing 32-bit x86 emulator images for new versions of Android, so Briefcase doesn't include x86 in the `abiFilters` anymore. I recommend you use x86_64 images instead, but if you want to stay on x86, you can still add it to the `abiFilters` line manually. None of the other things you mentioned should be necessary, but we're getting off the topic of the original question now, so if you need more help, please create an issue at the [Briefcase GitHub page](https://github.com/beeware/briefcase/issues). – mhsmith Nov 03 '22 at 10:04