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

Application lifetime events (suspend audio on Android) #10158

Merged
merged 6 commits into from
Oct 23, 2023

Conversation

mockersf
Copy link
Member

Objective

  • Handle pausing audio when Android app is suspended

Solution

  • This is the start of application lifetime events. They are mostly useful on mobile
  • Next version of winit should add a few more
  • When application is suspended, send an event to notify the application, and run the schedule one last time before actually suspending the app
  • Audio is now suspended too 🎉
audio.mp4

@mockersf mockersf added the O-Android Specific to the Android mobile operating system label Oct 17, 2023
@mockersf mockersf added this to the 0.12 milestone Oct 17, 2023
@alice-i-cecile alice-i-cecile added A-Audio Sounds playback and modification A-Windowing Platform-agnostic interface layer to run your app in labels Oct 17, 2023
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very useful fix, with good support for alternative audio backends. I like the straightforward lifetime events pattern, and I look forward to seeing how this evolves in the future.

Non-blocking, but does it make more sense to use a State rather than and Event for the Lifetime information? It feels like users may want to know about the current state, rather than just the transitions.

@mockersf
Copy link
Member Author

On most platforms, this is done by callbacks which translates more easily to events. Adding a state later if there's need for it should be easy enough.

@alice-i-cecile alice-i-cecile mentioned this pull request Oct 17, 2023
examples/mobile/src/lib.rs Outdated Show resolved Hide resolved
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leinan's concerns about resuming audio paused for other reasons are blocking for me. Good catch!

@mockersf
Copy link
Member Author

the code resuming audio is in the example code, and is not general. it uses single() anyway so it will panic if there are more than one audio source.

Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I can live with that. We can improve it later.

crates/bevy_window/src/event.rs Outdated Show resolved Hide resolved
crates/bevy_winit/src/lib.rs Outdated Show resolved Hide resolved
) {
for event in lifetime_events.read() {
match event {
Lifetime::Suspended => music_controller.single().pause(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we gate these only for Android builds?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they will start to be useful on iOS with next version of winit, and they don't hurt for now

crates/bevy_window/src/event.rs Outdated Show resolved Hide resolved
mockersf and others added 3 commits October 21, 2023 20:40
Co-Authored-By: Marco Buono <418473+coreh@users.noreply.github.com>
@mockersf mockersf added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Oct 21, 2023
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize)
)]
pub enum ApplicationLifetime {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bikeshedding heavily here, but ApplicationLifetime seems to suggest this is not an event but a config option of some kind? ApplicationLifetimeEvent is more verbose, but more explicit in what it's denoting.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to avoid events called ...Event. The variants makes it clear enough what it is for me

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Oct 23, 2023
Merged via the queue into bevyengine:main with commit 7d504b8 Oct 23, 2023
22 checks passed
ameknite pushed a commit to ameknite/bevy that referenced this pull request Nov 6, 2023
)

# Objective

- Handle pausing audio when Android app is suspended

## Solution

- This is the start of application lifetime events. They are mostly
useful on mobile
- Next version of winit should add a few more
- When application is suspended, send an event to notify the
application, and run the schedule one last time before actually
suspending the app
- Audio is now suspended too 🎉 



https://github.com/bevyengine/bevy/assets/8672791/d74e2e09-ee29-4f40-adf2-36a0c064f94e

---------

Co-authored-by: Marco Buono <418473+coreh@users.noreply.github.com>
rdrpenguin04 pushed a commit to rdrpenguin04/bevy that referenced this pull request Jan 9, 2024
)

# Objective

- Handle pausing audio when Android app is suspended

## Solution

- This is the start of application lifetime events. They are mostly
useful on mobile
- Next version of winit should add a few more
- When application is suspended, send an event to notify the
application, and run the schedule one last time before actually
suspending the app
- Audio is now suspended too 🎉 



https://github.com/bevyengine/bevy/assets/8672791/d74e2e09-ee29-4f40-adf2-36a0c064f94e

---------

Co-authored-by: Marco Buono <418473+coreh@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Audio Sounds playback and modification A-Windowing Platform-agnostic interface layer to run your app in O-Android Specific to the Android mobile operating system S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

5 participants