toga icon indicating copy to clipboard operation
toga copied to clipboard

toga Sound

Open coolcoder613eb opened this issue 2 years ago • 18 comments

What is the problem or limitation you are having?

I would like to make an app, which plays sound effects on button press the only solution i could find was this.

Describe the solution you'd like

a sound playing api, with support for mp3 or wav

Describe alternatives you've considered

https://stackoverflow.com/questions/74120614/how-to-play-sound-in-an-android-app-created-by-beeware-using-python

Additional context

No response

coolcoder613eb avatar Dec 18 '22 03:12 coolcoder613eb

Thanks for the suggestion. Adding an API for playing sound is definitely on our longer term todo list; however, as always, if someone were to contribute a design and implementation of a cross-platform API for sound, we'd be happy to consider it.

freakboy3742 avatar Dec 19 '22 03:12 freakboy3742

If you only need your app to work on Android, then the solution in the StackOverflow link above should work.

For desktop platforms, there are already several cross-platform audio packages for Python. The one I've had the most requests for on Android is PyAudio (https://github.com/chaquo/chaquopy/issues/389), so it would be worth looking into adding Android and iOS support to that rather than creating something Toga-specific.

mhsmith avatar Dec 22 '22 10:12 mhsmith

I'd like a solution for ios

On Thu, 22 Dec 2022 at 21:18, Malcolm Smith @.***> wrote:

If you only need your app to work on Android, then the solution in the StackOverflow link above should work.

For desktop platforms, there are already several cross-platform audio packages for Python. The one I've had the most requests for on Android is PyAudio (chaquo/chaquopy#389 https://github.com/chaquo/chaquopy/issues/389), so it would be worth looking into adding Android and iOS support to that rather than creating something Toga-specific.

— Reply to this email directly, view it on GitHub https://github.com/beeware/toga/issues/1716#issuecomment-1362658689, or unsubscribe https://github.com/notifications/unsubscribe-auth/AW5VQRGUVLP5H55DC5EXMPTWOQTGZANCNFSM6AAAAAATCJQJWA . You are receiving this because you authored the thread.Message ID: @.***>

coolcoder613eb avatar Dec 25 '22 07:12 coolcoder613eb

could you make wheels of a sound module for ios and android?

On Sun, 25 Dec 2022 at 18:00, David Bruce @.***> wrote:

I'd like a solution for ios

On Thu, 22 Dec 2022 at 21:18, Malcolm Smith @.***> wrote:

If you only need your app to work on Android, then the solution in the StackOverflow link above should work.

For desktop platforms, there are already several cross-platform audio packages for Python. The one I've had the most requests for on Android is PyAudio (chaquo/chaquopy#389 https://github.com/chaquo/chaquopy/issues/389), so it would be worth looking into adding Android and iOS support to that rather than creating something Toga-specific.

— Reply to this email directly, view it on GitHub https://github.com/beeware/toga/issues/1716#issuecomment-1362658689, or unsubscribe https://github.com/notifications/unsubscribe-auth/AW5VQRGUVLP5H55DC5EXMPTWOQTGZANCNFSM6AAAAAATCJQJWA . You are receiving this because you authored the thread.Message ID: @.***>

coolcoder613eb avatar Dec 25 '22 07:12 coolcoder613eb

Unfortunately it'll be a few months before we have time to look at this, so you'll have to use the platform-specific APIs for now.

mhsmith avatar Dec 28 '22 22:12 mhsmith

I would like to be able to test though, and I don't have a mac. I do have an ios device though, and I can borrow a mac from someone, but I only meet him once a week.

On Thu, 29 Dec 2022 at 09:40, Malcolm Smith @.***> wrote:

Unfortunately it'll be a few months before we have time to look at this, so you'll have to use the platform-specific APIs for now.

— Reply to this email directly, view it on GitHub https://github.com/beeware/toga/issues/1716#issuecomment-1366963879, or unsubscribe https://github.com/notifications/unsubscribe-auth/AW5VQREDHT2NV3PNKECZ5SLWPS6U3ANCNFSM6AAAAAATCJQJWA . You are receiving this because you authored the thread.Message ID: @.***>

coolcoder613eb avatar Dec 29 '22 09:12 coolcoder613eb

how hard would it be to write a simple wrapper for each platforms sound API?

On Thu, 29 Dec 2022 at 20:31, David Bruce @.***> wrote:

I would like to be able to test though, and I don't have a mac. I do have an ios device though, and I can borrow a mac from someone, but I only meet him once a week.

On Thu, 29 Dec 2022 at 09:40, Malcolm Smith @.***> wrote:

Unfortunately it'll be a few months before we have time to look at this, so you'll have to use the platform-specific APIs for now.

— Reply to this email directly, view it on GitHub https://github.com/beeware/toga/issues/1716#issuecomment-1366963879, or unsubscribe https://github.com/notifications/unsubscribe-auth/AW5VQREDHT2NV3PNKECZ5SLWPS6U3ANCNFSM6AAAAAATCJQJWA . You are receiving this because you authored the thread.Message ID: @.***>

coolcoder613eb avatar Dec 29 '22 09:12 coolcoder613eb

The Android API in the above StackOverflow link is quite simple. I don't know what the corresponding iOS API would be: perhaps @freakboy3742 can suggest one.

mhsmith avatar Dec 29 '22 11:12 mhsmith

an API like

foo = Sound('foo.mp3')
foo.play()

coolcoder613eb avatar Dec 29 '22 11:12 coolcoder613eb

or

foo.play_anync()

and

foo.pause() # pause
foo.play_ansync()  # continue
foo.stop()

coolcoder613eb avatar Dec 29 '22 11:12 coolcoder613eb

You can write such a wrapper yourself if you want, but as I said above, my preference would be not to add a sound API to Toga itself, but to add mobile support to one of the existing cross-platform audio packages.

mhsmith avatar Dec 29 '22 11:12 mhsmith

For anyone reading in the future, on Windows at least, its possible to use Togas webview to play audio. Yet to try if the behavior is the same on other platforms.

ItsCubeTime avatar Apr 04 '23 19:04 ItsCubeTime

Further discussion: #2106. Miniaudio looks like a promising option, and we've already released it for Android, though not tested all of its features.

mhsmith avatar Sep 02 '23 22:09 mhsmith

Here is some example code of recording audio from the microphone on Android.

mhsmith avatar Sep 25 '23 17:09 mhsmith

Using Audiostream, available at https://audiostream.readthedocs.io/en/latest/, you would be able to stream bytes on all platforms and also record byte streams simultaneously. Adding another layer with guidance from https://stackoverflow.com/questions/35529520/how-to-convert-a-wav-file-to-bytes-like-object, you should be able to create and play WAV files. If support for other formats is required, that would necessitate more dependencies.

Audiostream is a component of Kivy that can be reused and WAV handling is part of the standard library. The question then arises: how should this functionality be integrated into Toga? Personally, I wouldn't mind having a standard record button element that can be added, or simply utilizing its functions even if it is not integrated into the interface.

I plan to do some audio recording for a project I'm currently working on and will test if this approach works.

NADOOITChristophBa avatar Mar 14 '24 18:03 NADOOITChristophBa

@NADOOITChristophBa It depends a little on how Kivy is using that component. Kivy and Toga don't use the same libraries to provide integration with system libraries - Kivy uses PyObjUS and PyJNIUS; Toga uses Rubicon ObjC and Chaquopy. There's a lot of similarities between the two interfaces - but they're just different enough that they're not "drop in" replacements for each other.

It might be possible to abstract the interface layer so that both a Kivy-compatible and Toga-compatible interface can be exposed as an option, or put a compatibility shim in place - but a lot more investigation is required.

freakboy3742 avatar Mar 14 '24 22:03 freakboy3742

@freakboy3742 Now, I might be missing something, but from the repository, there don't seem to be any requirements for this to run, except for Cython. The examples on how to use it are done with Kivy, but except for that, it seems to be completely standalone. This is developed by people who work with the Kivy project, but it does not seem to depend on any of its infrastructure.

NADOOITChristophBa avatar Mar 15 '24 08:03 NADOOITChristophBa

Looking closer, audiostream is a wrapper around SDL. That means the SDL library is doing all the heavy lifting for audio access.

IIRC SDL is a library that Kivy provides as part of its stack, but at this time, BeeWare doesn't.

freakboy3742 avatar Mar 15 '24 10:03 freakboy3742