Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toga Sound #1716

Open
coolcoder613eb opened this issue Dec 18, 2022 · 18 comments
Open

toga Sound #1716

coolcoder613eb opened this issue Dec 18, 2022 · 18 comments
Labels
enhancement New features, or improvements to existing features.

Comments

@coolcoder613eb
Copy link

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 coolcoder613eb added the enhancement New features, or improvements to existing features. label Dec 18, 2022
@freakboy3742
Copy link
Member

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
Copy link
Member

mhsmith commented Dec 22, 2022

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), so it would be worth looking into adding Android and iOS support to that rather than creating something Toga-specific.

@coolcoder613eb
Copy link
Author

coolcoder613eb commented Dec 25, 2022 via email

@coolcoder613eb
Copy link
Author

coolcoder613eb commented Dec 25, 2022 via email

@mhsmith
Copy link
Member

mhsmith commented Dec 28, 2022

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
Copy link
Author

coolcoder613eb commented Dec 29, 2022 via email

@coolcoder613eb
Copy link
Author

coolcoder613eb commented Dec 29, 2022 via email

@mhsmith
Copy link
Member

mhsmith commented Dec 29, 2022

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
Copy link
Author

coolcoder613eb commented Dec 29, 2022

an API like

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

@coolcoder613eb
Copy link
Author

or

foo.play_anync()

and

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

@mhsmith
Copy link
Member

mhsmith commented Dec 29, 2022

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
Copy link
Contributor

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
Copy link
Member

mhsmith commented Sep 2, 2023

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
Copy link
Member

mhsmith commented Sep 25, 2023

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

@NADOOITChristophBa
Copy link

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
Copy link
Member

@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
Copy link

@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
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New features, or improvements to existing features.
Projects
None yet
Development

No branches or pull requests

5 participants