beeware / toga

A Python native, OS native GUI toolkit.
https://toga.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
4.37k stars 674 forks source link

toga Sound #1716

Open coolcoder613eb opened 1 year ago

coolcoder613eb commented 1 year ago

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

freakboy3742 commented 1 year ago

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.

mhsmith commented 1 year ago

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.

coolcoder613eb commented 1 year ago

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 commented 1 year ago

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: @.***>

mhsmith commented 1 year ago

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.

coolcoder613eb commented 1 year ago

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 commented 1 year ago

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: @.***>

mhsmith commented 1 year ago

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.

coolcoder613eb commented 1 year ago

an API like

foo = Sound('foo.mp3')
foo.play()
coolcoder613eb commented 1 year ago

or

foo.play_anync()

and

foo.pause() # pause
foo.play_ansync()  # continue
foo.stop()
mhsmith commented 1 year ago

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.

ItsCubeTime commented 1 year ago

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.

mhsmith commented 1 year ago

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 commented 1 year ago

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

NADOOITChristophBa commented 8 months ago

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.

freakboy3742 commented 8 months ago

@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.

NADOOITChristophBa commented 8 months ago

@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.

freakboy3742 commented 8 months ago

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.